Django 三个模型应该连接并依赖于其他
Django three model which should be connected and depends of other
我不知道如何存储/定义正确的模型/关系。
我有以下数据:
CATEGORY 是他们自己的模型,因为我喜欢在某些情况下 仅 查看/请求类别。但是对于这个类别,我想要设置值,我只能根据 YYYY-MM 定义一次。
是什么让我难以实现/理解我如何获得 VALUE <-> YYYY-MM 之间的关系,但仍然只保持对一个 CATEGORY“租金”的连接/依赖。
我会考虑做这样的事情。您有两个带有外键的模型,value 和 YYYY-MM 是带有外键的模型的属性。
class Category(models.model):
pass
class TimePeriod(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
value = models.IntegerField()
start_date = models.DateField()
class Meta:
constraints = [
models.UniqueConstraint(
fields=["category", "start_date"],
name="unique_start_date_per_category"
),
]
编辑:添加 UniqueConstraint
我不知道如何存储/定义正确的模型/关系。
我有以下数据:
CATEGORY 是他们自己的模型,因为我喜欢在某些情况下 仅 查看/请求类别。但是对于这个类别,我想要设置值,我只能根据 YYYY-MM 定义一次。
是什么让我难以实现/理解我如何获得 VALUE <-> YYYY-MM 之间的关系,但仍然只保持对一个 CATEGORY“租金”的连接/依赖。
我会考虑做这样的事情。您有两个带有外键的模型,value 和 YYYY-MM 是带有外键的模型的属性。
class Category(models.model):
pass
class TimePeriod(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
value = models.IntegerField()
start_date = models.DateField()
class Meta:
constraints = [
models.UniqueConstraint(
fields=["category", "start_date"],
name="unique_start_date_per_category"
),
]
编辑:添加 UniqueConstraint