我有个这样的模特:
class BlockedItem(models.Model):
name = models.CharField(max_length=244)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
def __unicode__(self):
return self.namecontent_type获取所有模型。我能在content_type中只给出想要的模特吗?object_id中,有什么方法可以从列表中选择而不是输入object_id?发布于 2014-08-01 12:16:13
如果您只想在content_type中使用某些模型,则可以使用
limit_choices = models.Q(app_label = 'myapp', model = 'MyModel') | models.Q(app_label = 'myotherapp', model = 'MyModelOtherModel') )
content_type = models.ForeignKey(ContentType, limit_choices_to = limit_choices )https://stackoverflow.com/questions/25078673
复制相似问题