On Jul 24, 5:46 am, Mike Dewhirst <mi...@dewhirst.com.au> wrote:
> I'm new to Django and want to express a self-referencing design
> feature to link clients in the same table with each other. Django is
> currently objecting to this. Could you please point me to
> documentation which will let me figure it out?
>
> This is my models.py in a play project using Django from svn rev ...
>
> - - - - - - - - - - -
> from django.db import models
>
> def when():
>     return datetime.now()
>
> class Client(models.Model):
>     """ base class for everyone/thing """
>     surname = models.CharField(max_length=48, blank=False)
>     client_type = models.CharField(max_length=24, blank=False)
>     def __unicode__(self):
>         return self.client_type + ": " + self.surname
>
> class Membership(models.Model):
>     """ cross (xr) referencing and linking clients """
>     client_x = models.ForeignKey(Client)
>     client_r = models.ForeignKey(Client)
>     def __unicode__(self):
>         return str(self.client_x + " : " + str(self.client_r)
>
> - - - - - - - - - - -

You don't say what the error is. I can guess, though, and as always
the clue is in the error message (why do people never seem to read
these?)

You've got two foreign keys from membership to client. Django
automatically creates a backwards relationship from client to
membership, and it usually calls it  'foo_set' where foo is the lower-
case name of the related model. Since you've got two relationships,
though, Django wants to create two attributes called 'membership_set'
on the Client model, which obviously isn't possible. So to solve this,
do exactly what the error message says - add a 'related_name'
parameter on the ForeignKey definition:

    client_x = models.ForeignKey(Client,
related_name='membership_x_set')
    client_r = models.ForeignKey(Client,
related_name='membership_r_set')

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