Model可以使用两个模型之一的ForeignKey

问题描述 投票:2回答:1

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

我们可能就整栋大楼或该大楼内的单一物业签订租约。在前一种情况下,租赁文件适用于建筑物,在后一种情况下,它们仅适用于财产。

我使用content_types添加通用外键但现在我无法弄清楚如何将自动填充字段添加到contenttype中,在下拉列表中,我只是看到了admin表单中的building和property。我想看看建筑物名称和属性名称。

我在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',)
django django-models django-admin django-contenttypes django-2.0
1个回答
1
投票

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

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

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()
© www.soinside.com 2019 - 2024. All rights reserved.