On 11 April 2013 16:19, Lachlan Musicman <data...@gmail.com> wrote:
> Hi,
>
> I'm trying to implement a simple family tree type structure. I'm
> expanding on this example code base:
>
> https://github.com/mieows/familytree-django/blob/master/models.py
>
> (please ignore any errors in that models.py, I've addressed many of them)
>
> Ok, simply I have:
>
> class Account(models.Model)

Gah, sorry.

class Account(models.Model):
  user = models.OneToOneField(User, blank=True, null=True)
  first_name = models.CharField(max_length=60)
  last_name = models.CharField(max_length=60)

  # Relations
  parents = models.ManyToManyField("self", related_name='p',
verbose_name="Parents", null=True, blank=True)
  siblings = models.ManyToManyField("self", related_name='s',
verbose_name="Siblings", null=True, blank=True)
  partners = models.ManyToManyField("self", related_name='ps',
verbose_name="Partners", null=True, blank=True)
  children = models.ManyToManyField("self", related_name='c',
verbose_name="Children", null=True, blank=True)

So, in Relations we have two type of relationships, symmetrical and
non symmetrical.

ie:
parents<->children is non symmetrical
partners<->partner is symmetrical
siblings<->siblings is symmetrical

Here's where the confusion comes in. As per the original code base I
have used the m2m_changed to make sure the adjustments are reflected.
With the Parent<->children this has worked as I expected (
jack.parents.add(jill) also reflects that jack is in
jill.children.all() )

But with the other two relationships, the m2m_changed are not working.

Since both testing and m2m_changed/signals are new to me, my brain is
melting a little.

1. a) Should I explicitly have symmetrical=FALSE in the
parents/children relationships? ie:
 parents = models.ManyToManyField("self", symmetrical=FALSE,
related_name='p', verbose_name="Parents", null=True, blank=True)
 siblings = models.ManyToManyField("self", symmetrical=FALSE,
related_name='s', verbose_name="Siblings", null=True, blank=True)

1. b) what effect would that have? At the moment I have tables on the database:

account_account
account_account_parents
account_account_children
account_account_partners
account_account_siblings

2. I am trying to test the reflected removals of partners and siblings
and the tests are failing. So I moved to the shell, and I'm seeing
this:

>>> bob = Account(first_name="bob",last_name="sanders",gender='M')
>>> sarah = Account(first_name="sarah",last_name="sanders",gender='F')
>>> bob.siblings.add(sarah)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File 
"/home/datakid/.virtualenvs/kids/local/lib/python2.7/site-packages/django/db/models/fields/related.py",
line 897, in __get__
    through=self.field.rel.through,
  File 
"/home/datakid/.virtualenvs/kids/local/lib/python2.7/site-packages/django/db/models/fields/related.py",
line 586, in __init__
    (instance, source_field_name))
ValueError: "<Account: bob sanders>" needs to have a value for field
"from_account" before this many-to-many relationship can be used.
>>>

That's where my mind went to jelly. I can see that the error message
contains a generated string - "from_account" - but I don't see
anything in the docs that would talk about from_CLASS settings.

What am I doing wrong? Is it that I've not added the + to
related_name? Is it that I've using m2m_changed on a symmetrical
relationship (I've just commented out the m2m_changed sibling code and
tried again, but it fails with the same message).

Confused.

cheers
L.




--
The new creativity is pointing, not making. Likewise, in the future,
the best writers will be the best information managers.

http://www.theawl.com/2013/02/an-interview-with-avant-garde-poet-kenneth-goldsmith

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to