Den 13/03/2014 kl. 06.34 skrev Santiago Palacio Gómez <spala...@gmail.com>:

> Hi everyone,
> 
> I'm a Django newbie, and I was following the tutorial for 1.6, creating a new 
> app where one of the models is self-referencing. They ask to, in manage.py 
> shell, create an instance of the model. But, as my model has a self reference 
> foreign key, I can't pass any object to the key. I also would not like to 
> leave it Nullable, as it may cause errors. Rather, I'd just like to pass 
> 'self' as the reference, this is, the key it references should be its self 
> key.
> 
> Is it possible? Thanks in advance!

What is meant in the tutorial is most definitely not that the model 'instance' 
should reference itself, but rather that the instance should reference another 
instance of the same type, i.e. a parent-child relationship. Something like:

class Person(models.Model):
  mother = models.ForeignKey('self', null=True)
  father = models.ForeignKey('self', null=True)

You need to keep the field nullable because something must be root of the 
relationship structure (unless you want to create only cyclic relationships, 
which is not recommended for family structures...). To create a relationship do 
this:

eve = Person.objects.create()
adam = Person.objects.create()
abel = Person.objects.create(mother=eve, father=adam)


Erik

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/23D801C1-ED14-40B2-B241-588FD3F29709%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to