Re: Reverse Query Name Clash?

2010-11-04 Thread r_f_d
This is actually pretty well documented in the django docs.  I suggest
you look there for a complete explanation, but briefly: with your
model definition, the way you would access a minister's church would
be: person_object.church and the  way you would access the church a
person attends would also be person_object.church.  Django cannot know
the context of what you are trying to get at and therefore does not
allow 'reverse' relations with identical names.  Changing your code
to:

minister = models.ForeignKey(Person, related_name='pulpit', null=True,
blank=True)

removes the conflict allowing you to access a ministers church via
person_object.pulpit and the church a person attends via
person_object.church

obviously, you do not need to use the pulpit, just something other
than church, you could also change the related_name on the Person:

church = models.ForeignKey(Church, related_name='home_church')

and then use person_object.church to get a 'ministers' church and
person_object.home_church to get the church a layman attends.
Whatever makes more sense to you.

One other thing, I am not sure of your exact use-case but you might
want to think about making the FK for ministers a M2M just in-case you
have a Church with multiple ministers.  If it is important to track
the type of minister (Senior, Associate, Youth, Music, etc...) you
might want to look at using the "through" option of the m2m field
definition, consult the docs for more information:
http://docs.djangoproject.com/en/1.2/topics/db/models/#extra-fields-on-many-to-many-relationships

hth

On Nov 1, 9:40 pm, Victor Hooi  wrote:
> Hi,
>
> I'm getting a error about reverse query name clashes with my models.
>
> We have a Django app to manage conferences and conference attendees.
>
> In our models.py, two of the models we have are:
>
> 1. Person, representing people attending people attending a
> conference. Each person also has a "church" field, which represents
> the main church they attend.
>
>     class Person(models.Model):
>         first_name = models.CharField(max_length=50)
>         last_name = models.CharField(max_length=50)
>         gender = models.CharField(max_length=1,
> choices=GENDER_CHOICES)
>         spouse = models.ForeignKey('self', null=True, blank=True)
>         date_of_birth = models.DateField()
>         church = models.ForeignKey('Church')
>         ...
>
> The "church" FK is in quotation marks, since the Church object is
> defined below Person.
>
> 2. "Church", which defines a church, and includes an optional field
> for the main minister at that church. The minister field is a FK to a
> Person.
>
> class Church(models.Model):
>     name = models.CharField(max_length=50)
>     address = models.CharField(max_length=50)
>     suburb = models.CharField(max_length=30)
>     postcode = models.IntegerField()
>     state = models.CharField(max_length=3, choices=AUSTRALIAN_STATES)
>     minister = models.ForeignKey(Person, null=True, blank=True)
>
> So a person has a church, and a church also has a minister (in most
> cases the two will be different, except for the case where the
> minister themselves is attending a conference, which should of course
> be valid).
>
> The issue here is that the model doesn't validate:
>
>     Error: One or more models did not validate:
>     conferences.church: Reverse query name for field 'minister'
> clashes with field 'Person.church'. Add a related_name argument to the
> definition for 'minister'.
>
> Now, if I change the name of the "church" field under Person, it will
> validate - however, I'm still curious as to why this doesn't work? Any
> way to fix it? (I assume I could add a related_name argument, I'm just
> trying to figure out what's going on, and gain more understanding for
> Django).
>
> Cheers,
> Victor

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Reverse Query Name Clash?

2010-11-01 Thread Victor Hooi
Hi,

I'm getting a error about reverse query name clashes with my models.

We have a Django app to manage conferences and conference attendees.

In our models.py, two of the models we have are:

1. Person, representing people attending people attending a
conference. Each person also has a "church" field, which represents
the main church they attend.

class Person(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
gender = models.CharField(max_length=1,
choices=GENDER_CHOICES)
spouse = models.ForeignKey('self', null=True, blank=True)
date_of_birth = models.DateField()
church = models.ForeignKey('Church')
...

The "church" FK is in quotation marks, since the Church object is
defined below Person.

2. "Church", which defines a church, and includes an optional field
for the main minister at that church. The minister field is a FK to a
Person.

class Church(models.Model):
name = models.CharField(max_length=50)
address = models.CharField(max_length=50)
suburb = models.CharField(max_length=30)
postcode = models.IntegerField()
state = models.CharField(max_length=3, choices=AUSTRALIAN_STATES)
minister = models.ForeignKey(Person, null=True, blank=True)

So a person has a church, and a church also has a minister (in most
cases the two will be different, except for the case where the
minister themselves is attending a conference, which should of course
be valid).

The issue here is that the model doesn't validate:

Error: One or more models did not validate:
conferences.church: Reverse query name for field 'minister'
clashes with field 'Person.church'. Add a related_name argument to the
definition for 'minister'.

Now, if I change the name of the "church" field under Person, it will
validate - however, I'm still curious as to why this doesn't work? Any
way to fix it? (I assume I could add a related_name argument, I'm just
trying to figure out what's going on, and gain more understanding for
Django).

Cheers,
Victor

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.