On Fri, Jul 17, 2009 at 6:55 AM, Oleg Oltar<oltarase...@gmail.com> wrote:

> I want to create an object that contains 2 links to users. For example
>
> class GameClaim(models.Model):
>     target = models.ForeignKey(User)
>     claimer = models.ForeignKey(User)
>     isAccepted = models.BooleanField()
>
> but I am getting an error when running server:
>
> Accessor for field 'target' clashes with related field 'User.gameclaim_set'.
> Add a related_name argument to the definition for 'target'.
> Accessor for field 'claimer' clashes with related field
> 'User.gameclaim_set'. Add a related_name argument to the definition for
> 'claimer'.
>
> Please explain why I am getting the error and how to fix it?

When you create a relationship from model A (GameClaim) to model B
(User), Django automatically adds to model B isntances Python attribute
(the ' accessors'  above) that allow you to in case you have one B instance,
to get all the A instances related to it (in your example, using the claimer FK,
it would allow you to, starting from an given user, obtain all the
games she/he claimed).

The name of such an accessor is, by default, determined by a simple
rule: the A name lowercased and suffixed with '_set' (gameclaim_set).

The problem in your case is a typical one and the model validation error
messages you are getting are pointing you in the right direction: you have
two FKs between A and B and then the default names for the accessors
Django is trying to add clash.

Is In those cases when the related_name parameter of the ForeignKey
field is useful because it allows you to override the default accessor
name 'gameclaim_set'.

Read the relevant documentation for more details:

http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
http://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-related-objects

HTH.

-- 
Ramiro Morales
http://rmorales.net

PyCon 2009 Argentina - Vie 4 y Sab 5 Septiembre
Buenos Aires, Argentina
http://ar.pycon.org/2009/about/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to