Accessor for field 'somefield' clashes with related field ... Add a related_name argument to the definition for 'somefield'

2006-12-23 Thread [EMAIL PROTECTED]


Hi... I was wondering why when you have something like this:
class Loan(models.Model):
   loan_id = models.AutoField(primary_key=True)
   originator =models.ForeignKey(UserInfo,null=True)
   obligor = models.ForeignKey(UserInfo,null=True)

Why it gives you errors..until you change it to this:
class Loan(models.Model):
   loan_id = models.AutoField(primary_key=True)
   originator =models.ForeignKey(UserInfo,
related_name="originator",null=True)
   obligor = models.ForeignKey(UserInfo,
related_name="obligor",null=True)



Why doesn't django just just look at the name you have specified before
the equal sign..rather than making you put the same thing in a
related_name field?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Accessor for field 'somefield' clashes with related field ... Add a related_name argument to the definition for 'somefield'

2006-12-24 Thread Malcolm Tredinnick


On Wed, 2006-12-20 at 06:00 +, [EMAIL PROTECTED] wrote:

Hi... I was wondering why when you have something like this:
class Loan(models.Model):
loan_id = models.AutoField(primary_key=True)
originator =models.ForeignKey(UserInfo,null=True)
obligor = models.ForeignKey(UserInfo,null=True)

Why it gives you errors..until you change it to this:
class Loan(models.Model):
loan_id = models.AutoField(primary_key=True)
originator =models.ForeignKey(UserInfo,
related_name="originator",null=True)
obligor = models.ForeignKey(UserInfo,
related_name="obligor",null=True)



Why doesn't django just just look at the name you have specified before
the equal sign..rather than making you put the same thing in a
related_name field?


Two reasons, a technical Python one and a simpler Django one:

(1) At the time the ForeignKey class is created, it has no idea what it
is going to be assigned to (the name of the attribute). This is the
Python reason.

(2) The Django reason is because the default related name comes from the
model name. So if you are creating multiple relations to the same model
(UserInfo in this case) in one model, you must specify the related_name
in order to resolve the ambiguities (you only need to set the related
name on one of those fields, I suspect). Django never tries to guess
what you want to do when the defaults don't work. You choose to use the
field names, however, they are singular nouns and the reverse relation
is usually a set (a collection of multiple objects), so the singular
form is not necessarily the natural word to use here.

Hope that clarifies things a little bit.

Regards,
Malcolm


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---