What if that was what he wanted? Think about this case:

class Player:
    name = CharField()

class Game:
    player1 = ForeignKey(Player)
    player2 = ForeignKey(Player)

Now who want player.game_player1_set and player.game_player2_set?
Definetly player.game_set is the answer. But it's not possible.


On 7월17일, 오후8시23분, Ramiro Morales <[email protected]> wrote:
> On Fri, Jul 17, 2009 at 6:55 AM, Oleg Oltar<[email protected]> 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/#foreignkeyhttp://docs.djangoproject.com/en/dev/topics/db/queries/#backwards-rel...
>
> HTH.
>
> --
> Ramiro Moraleshttp://rmorales.net
>
> PyCon 2009 Argentina - Vie 4 y Sab 5 Septiembre
> Buenos Aires, Argentinahttp://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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to