On 8/16/07, Peter Melvyn <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> please, could anybody confirm my experience with related_names. I
> tried to find-out some information about it and found invalid model
> example.
>
> If my understanding is correct, then it is not possible to have the
> same related_names in two different models. Is it correct?
Put it this way: When you define a model, you specify a bunch of field
names. For (obvious) Python reasons, the field names must all be
unique.
When you have a ForeignKey or ManyToManyField, you have the option of
adding a 'related_name' argument; the name use specify effectively
gets added to the model that the related field is pointing at.
However, even though the 'related_name field' isn't actually present
in the model definition, the same uniqueness rules apply - a model
can't have multiple fields with the same name.
So: This is legal:
class FirstModel(Model):
name = CharField()
class SecondModel(Model):
name = CharField()
class ThirdModel(Model):
field1 = ForeignKey(FirstModel, related_name='foo')
field2 = ForeignKey(SecondModel, related_name='foo')
because FirstModel will get a 'foo' attribute, and SecondModel will
get a 'foo' attribute, but neither causes a name clash. However:
class FirstModel(Model):
field1 = ForeignKey(ThirdModel, related_name='foo')
name = CharField()
class SecondModel(Model):
field2 = ForeignKey(ThirdModel, related_name='foo')
class ThirdModel(Model):
name = CharField()
is NOT legal, because ThirdModel would need to have two fields called 'foo'.
So - you can use the same related name multiple times - BUT - the same
related name can't be added to a single model more than once.
Hope this clarifies things a bit.
Yours,
Russ Magee %-)
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---