我尝试使用grappelli来获取通用外键的小部件。这就是我需要得到的:http://code.google.com/p/django-grappelli/wiki/generic_2_1。 我有一个模特:
class Circuit(models.Model):
circuit_id = models.PositiveIntegerField(verbose_name=_("Circuit ID"), blank=True, null=True)
model = models.ForeignKey(Versiiplates, verbose_name=_("Model"))
date_created = models.DateField(blank=True, null=True, verbose_name=_("Date Created"), default=datetime.date.today())
manufacturer = models.ForeignKey(Manufacturer, null=True, blank=True, verbose_name="Производитель")
manufacturer_comments = models.TextField(max_length=2500, null=True, blank=True, verbose_name="Комментарии производителя")
keeper_type = models.ForeignKey(ContentType, blank=True, null=True, related_name="keeper_type")
keeper_id = models.PositiveIntegerField(blank=True, null=True)
keeper = generic.GenericForeignKey('keeper_type', 'keeper_id')
status = models.PositiveIntegerField(verbose_name=_("Status"), choices=STATUS_TYPE, default=0)
status_history = models.ManyToManyField(Status, blank=True, null=True)
status_comment = models.TextField(max_length=2500, blank=True, null=True, verbose_name="Комментарий")
def __unicode__(self):
return u"%s #%d" % (self.model, self.circuit_id)
class Meta:
verbose_name = _("Circuit")
verbose_name_plural = _("Circuits")
和AdminModel:
class CircuitAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['circuit_id', 'model', 'date_created']}),
(_("Manufacturer"), {'fields':['manufacturer', 'manufacturer_comments']}),
(_("Status"), {'fields': ['status', 'status_comment']})
]
list_display = ('circuit_id', 'model', 'date_created', 'keeper', 'status')
search_fields = ('circuit_id', )
list_filter = ('model', 'status')
related_lookup_fields = {
'generic': [['keeper_type', 'keeper_id']]
}
但是我的GenericForeignKey在管理员中仍然没有小部件。为什么在grapelli的使用它的例子中有两个GenericForeignKey字段?
答案 0 :(得分:0)
答案很简单。我只需要添加keeper_id和keeper_type字段集,如下所示:
fieldsets = [
(None, {'fields': ['circuit_id', 'model', 'date_created', 'keeper_type', 'keeper_id']}),
(_("Manufacturer"), {'fields':['manufacturer', 'manufacturer_comments']}),
(_("Status"), {'fields': ['status', 'status_comment']})
]
答案 1 :(得分:0)
对于更新的答案,现在似乎是正确的方式:
class ProjectTreeAdmin(MPTTModelAdmin):
related_lookup_fields = {
'generic': [['content_type', 'object_id']],
}
从this question看,指向the doc