I can't wrap my head how to store / define the right model / relationship.
I have following data:
CATEGORY is their own model because i like to view / request in some situation only the category. But to this category I want set VALUE which I can only define onetime depending ON YYYY-MM.
What make me difficult to achieve / understand how i get the relationship between VALUE <-> YYYY-MM but still maintain the connection / dependency to only one CATEGORY "Rent".
CodePudding user response:
I would look at doing something like this. You have two models with a foreign key, and value and YYYY-MM are attributes of the model with the foreign key.
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"
),
]
EDIT: Add UniqueConstraint