views:

130

answers:

1

Hi SO,

This is probably a db design issue, but I couldn't figure out any better. Among several others, I have these models:

class User(models.Model):
  name = models.CharField( max_length=40 )
  # some fields omitted
  bands = models.ManyToManyField( Band )

and

class Band(models.Model):
  creator = models.ForeignKey( User )
  # some fields omitted
  name = models.CharField( max_length=40 )

So basically, I've got a user entity, which has many to many relation with a band entity. The twist is that I want a special user, who "created" the band on the site to have special editing capabilities. So I went forward, and added a ForeignKey called creator. The code could not run, because Band came after User in the source. So I forward declared class Band(models.Model): pass. Sadly, this does not seem to be exatly a good idea, because now Band is the only model that doesn't show up any interface elements in the django admin (Bands model is there, it just can't be edited).

My question is that what change should I make in the models to get this work properly? (if any)

Thank you in advance, sztomi

+4  A: 

See: http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey, which says:

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:

 class Car(models.Model):
      manufacturer = models.ForeignKey('Manufacturer')
      # ...

 class Manufacturer(models.Model):
      # ...
thedz
Awesome, thanks. Django FTW!
Tamás Szelei