模型可以有一个具有两个模型之一的外键

Model can have a ForeignKey with one of the two models

我需要一些帮助来解决我自己无法解决的问题。所以在这个模型中,租赁文件可以有一个 ForeignKey with Building 或 属性.

我们可能对整栋建筑或该建筑内的单个 属性 签订了租赁协议。在前一种情况下,租赁文件适用于建筑物,而在后一种情况下,它们仅适用于 属性.

我使用 content_types 添加通用外键,但现在我不知道如何将自动完成字段添加到 contenttype 和下拉列表中, 我只看到管理表单中的建筑和 属性。我想查看建筑物名称和 属性 名称。

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

models.py:

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.py:

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

似乎通用外键总是比它的价值更麻烦。它采用了一个简单的概念,一个关系连接,并试图让它变得聪明,但是像自动完成这样的下游包将无法工作。

我最终切换到两个单独的外键,然后将属性添加到 class 以从正确的相关记录中提取字段。

class TenancyDocument(models.Model):
    building = models.ForeignKey(Building, ondelete='CASCADE', null=True, blank=True)
    prop = models.ForeignKey(Property, ondelete='CASCADE', null=True, blank=True)

    def clean(self):
        if not self.building and not self.prop:
            raise ValidationError('Must provide either building or property.')
        if self.building and self.prop:
            raise ValidationError('Select building or property, but not both.')
        super().clean()