#2864 closed defect (invalid)
Creating a ManyToManyField - order of classes
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 0.95 |
Severity: | major | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If you create a ManyToMany field in a class BEFORE the class being referenced and then use "manage.py syncdb" you get an error. The samples below show exactly the same classes. However, the first example works and the second does not. The order of the classes is important. This is either a bug or if it isn't there should be something in the documentation. I wasted a couple of hours trying to figure out what was wrong.
THIS WORKS
from django.db import models
import datetime
# Create your models here.
class Promoter(models.Model):
promoter_name = models.CharField(maxlength=200)
class Admin:
pass
class Category(models.Model):
category_name = models.CharField(maxlength=400)
class Admin:
pass
class Promotion(models.Model):
promoter_id = models.ForeignKey(Promoter)
categories = models.ManyToManyField(Category)
promo_headline=models.CharField(maxlength=300)
class Admin:
pass
THIS DOES NOT WORK (Classes in different order)
from django.db import models
import datetime
# Create your models here.
class Promoter(models.Model):
promoter_name = models.CharField(maxlength=200)
class Admin:
pass
class Promotion(models.Model):
promoter_id = models.ForeignKey(Promoter)
categories = models.ManyToManyField(Category)
promo_headline=models.CharField(maxlength=300)
class Admin:
pass
class Category(models.Model):
category_name = models.CharField(maxlength=400)
class Admin:
pass
merric,
Are you sure you've read the documentation?
Yo can find the explanation and an example for this topic in
http://www.djangoproject.com/documentation/model_api/#relationships
"If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself:..."
I'm closing this ticket.