使用Model.objects.all()作为辅助表项的蓝图

2024-10-02 10:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一点问题的逻辑应该如何工作,所以我希望这是可能的

我想出了一个可能的解决方案,写在下面作为答案,我会在几天后接受它,但如果有人提出更好的解决方案,我会否定我发布的任何答案。

总的来说,我正在Django开发一个公寓迁出/迁入检查应用程序,在这两部分中,我都有通用位置,每个报告都必须检查这些位置。我允许客户机更新/提交InspectionLocations对象,这在如何将提交的报告存储在我的数据库中提出了一个问题

我想要的是使用InspectionLocations表作为蓝图,为Move-Ins构建检查报告,其中表单字段是基于InspectionLocations对象locationstatusinformation属性/字段生成的

我的问题就在这一点上,当InspectionLocations中的字段数量可以更改时,如何引用这些值作为构建报告提交的蓝图

from django.db import models
from apps.units.models import Unit

class Inspections(models.Model):
    class Meta:
        abstract = True
    id = models.AutoField(primary_key=True)
    inspection_date = models.DateField()


    submitted_by = models.ForeignKey(
        'users.CustomUser',
        default=None,
        null=True,
        on_delete=models.SET_NULL,
        db_column='submitted_by')

    last_update = models.DateTimeField(auto_now=True)
    date_added = models.DateTimeField(auto_now_add=True, editable=False)


class MoveInInspections(Inspections):
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE, db_column='unit_id')
    # should have reference to all InspectionLocation items as reference for submission, how?



class MoveOutInspections(Inspections):
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE, db_column='unit_id')
    date_notice_given = models.DateField(blank=True, null=True, default=None)
    date_vacated = models.DateField(blank=True, null=True, default=None)
     # should have reference to all InspectionLocation items as reference for submission, how?



class InspectionLocations(models.Model):
    '''

    Defualt Inspection Locations are created when a 
    client is created using code like this:

    InspectionLocation.objects.get_or_create(location='Living Room')
    InspectionLocation.objects.get_or_create(location='Dining Room')
    InspectionLocation.objects.get_or_create(location='Kitchen')
    InspectionLocation.objects.get_or_create(location='Bedroom')
    InspectionLocation.objects.get_or_create(location='Bathroom')
    InspectionLocation.objects.get_or_create(location='Other')
    '''

    id = models.AutoField(primary_key=True)
    location = models.CharField(max_length=50)
    status = models.BooleanField(default=None)
    information = models.TextField(default=None, blank=True)

我尝试了许多字段和FK,但我似乎无法让逻辑工作,因为每当对象引用InspectionLocations对象时,它都会普遍更改每个报表的数据,这导致了我需要以某种方式将其用作蓝图的想法


Tags: or对象noneidtruedefaultdbget
1条回答
网友
1楼 · 发布于 2024-10-02 10:24:04

我没有在我的问题中发布这个,因为它越来越长了,但到目前为止,我最好的选择似乎是使用Django JSONField(正如我使用Postgres一样),如下所示:

from django.contrib.postgres.fields import JSONField

class MoveInInspections(Inspections):
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE, db_column='unit_id')
    data = JSONField()



class MoveOutInspections(Inspections):
    unit = models.ForeignKey(Unit, on_delete=models.CASCADE, db_column='unit_id')
    date_notice_given = models.DateField(blank=True, null=True, default=None)
    date_vacated = models.DateField(blank=True, null=True, default=None)
    data = JSONField()

我将InspectionLocations对象的值存储在字典中的位置

相关问题 更多 >

    热门问题