0

我尝试使用 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 字段?

4

2 回答 2

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']})
    ]
于 2013-02-21T09:54:28.843 回答
0

对于更新的答案,正确的方法现在似乎是:

class ProjectTreeAdmin(MPTTModelAdmin):
    related_lookup_fields = {
        'generic': [['content_type', 'object_id']],
    }

这个问题来看,指向文档

于 2015-04-18T22:28:18.120 回答