使用与ContentType相关的字段自动完成

时间:2018-02-26 15:51:24

标签: django django-models django-forms django-templates django-rest-framework

我需要帮助解决一个我无法自行解决的问题。 因此,在此模型中,租赁文档可以具有带建筑物或属性的ForeignKey。

我无法弄清楚如何将自动填充字段添加到contenttype,在下拉列表中,我只是看到了admin格式的构建和属性。我想看看建筑物名称和属性名称。

我了解了Django 2.0中的自动填充字段,它很棒,但我不知道在这种特殊情况下如何使用类似的东西,或者有更好的方法可以做到这一点?

型号:

class TenancyDocument(models.Model):

    KINDS = Choices('Tenancy Agreement', 'Stamp Duty', 'Inventory List')

    id = FlaxId(primary_key=True)
    kind = StatusField(choices_name='KINDS')
    start_date = models.DateField(blank=True, null=True)
    end_date = models.DateField(blank=True, null=True)

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    content_type_limit = Q(
        app_label='properties', model='building') | Q(
            app_label='properties', model='property')

    content_type = models.ForeignKey(
        ContentType,
        limit_choices_to=content_type_limit,
        on_delete=models.CASCADE,
        verbose_name='Lease type'
        )

    object_id = FlaxId(blank=True, null=True)
    content_object = GenericForeignKey('content_type', 'object_id')

    def __str__(self):
        return self.kind

管理:

@admin.register(TenancyDocument)
class TenancyDocumnentAdmin(admin.ModelAdmin):
    list_display = ('id', 'kind', 'start_date', 'end_date','content_type')
    list_filter = ('kind',)

0 个答案:

没有答案
相关问题