转发声明 - django没有管理页面?

时间:2022-10-04 11:59:12

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).

所以基本上,我有一个用户实体,它与一个带实体有很多很多关系。扭曲的是我想要一个特殊的用户,他在网站上“创建”了乐队以获得特殊的编辑功能。所以我继续前进,并添加了一个名为creator的ForeignKey。代码无法运行,因为Band来自源中的User。所以我转发声明的类Band(models.Model):pass。遗憾的是,这似乎并不是一个好主意,因为现在Band是唯一没有在django admin中显示任何界面元素的模型(Bands模型在那里,它只是无法编辑)。

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

我的问题是,我应该在模型中做出哪些改变才能使这项工作正常进行? (如果有的话)

1 个解决方案

#1


12  

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

请参阅:http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey,其中说:

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):
      # ...

#1


12  

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

请参阅:http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey,其中说:

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):
      # ...