Suppose I have the following model:
class Cow (models.Model):
name = models.CharField(max_length=100)
color = models.CharField(max_length=100, default="brown")
On our farm we have many cows. Typically, the farmer takes cows to the vet in defined batch:
class Cow_Batch(models.Model):
batch_name = models.CharField(max_length=100)
cows = models.ManyToManyField(Cow)
In our admin, we can create a Cow_Batch, allowing our farmer to easily take Daisy and Debbie (two sisters with a similar medical condition) to the vet together.
I’d like to be able to have ‘all’ as a Cow_Batch. That’s easy enough to populate myself, but I’d like the contents of ‘all’ to update automatically when new cows are added to the herd. The end purpose is to be able to use:
class Vet_Visit(models.Model):
cows_brought_to_vet = models.ForeignKey(Cow_Batch)
and have ‘all’ as a valid possibility, among others.
I think you can make use of django post_save signal for
Cowmodel. In the signal handler when newCowis added, you add it to allCow_Batch.Django Signals