Re: model: how to reference to the model itself?
On 24/10/2011 4:00pm, Mike Dewhirst wrote:
> On 24/10/2011 3:47pm, Ken wrote:
>> I want to create a parent-child like catalog system.
>>
>> from django.db import models
>>
>> class Catalog(models.Model):
>> username = models.EmailField()
>> name = models.CharField(max_length=64)
>> color = models.CharField(max_length=20)
>> state = models.IntegerField()
>> parentcatalog = models.ManyToOneRel(Catalog)
>>
>> but when syncdb i got:
>> File "/home/ken/sites/mysite/catalog/models.py", line 12, in Catalog
>> parentcatalog = models.ManyToOneRel(Catalog)
>> NameError: name 'Catalog' is not defined
>>
>> It seems like I cannot ref to a model which is not completely defined.
>> What should I do?
... and once you get past that you will likely find there isn't a
models.ManyToOneRel class. You imported models from django so that is
where python will look for that class. You don't need to refer to your
own models.py namespace in front of your (incompletely defined)
ManyToOneRel() class.
I think you are looking for a models.ForeignKey('self') relationship.
https://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey
>
> With any luck you should be able to put Catalog in quotes so it is a
> string rather than an undefined object.
>
>>
>> --
>> 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+unsubscribe@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>
--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home