Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#2864 closed defect (invalid)

Creating a ManyToManyField - order of classes

Reported by: merric@… Owned by: adrian
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: UI/UX:

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

Attachments (0)

Change History (1)

comment:1 Changed 8 years ago by ramiro

  • Resolution set to invalid
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.