I have an app calendar that looks like this:
class Events(models.Model):
start_date = models.DateTimeField(null=True,blank=True)
end_date = models.DateTimeField(null=True,blank=True)
now I want a calendar event, also a reference to other models (A, B, C, ...). Now you could of course create your own models.ForeignKey
for each class:
class Events(models.Model):
start_date = models.DateTimeField(null=True,blank=True)
end_date = models.DateTimeField(null=True,blank=True)
a = models.ForeignKey('A', on_delete=models.CASCADE, blank=True, null=True)
b = models.ForeignKey('B', on_delete=models.CASCADE, blank=True, null=True)
c = models.ForeignKey('C', on_delete=models.CASCADE, blank=True, null=True)
...
but I find that somehow inefficient, as the model has to be adapted again and again for each extension. Therefore, my consideration would be that I create a member for the model itself and a field for the respective id of the model.
class Events(models.Model):
start_date = models.DateTimeField(null=True,blank=True)
end_date = models.DateTimeField(null=True,blank=True)
Table_id = ??? (A, B, C, ...)
Model_id = ??? (a1, a2, b1, c3, ...)
if I imagine that correctly, each model consists of its own table in the database (or several). Whereby the table entries of model A are then for example a1, a2, a3 ... Therefore I need a reference to the respective table and a reference to the table entry in order to carry out an exact referencing. how should you do that?
I've already thought about a kind of LUT, but I'm just postponing the problem ... django must have something ready, right?
Surely there is some trick there? or would you solve it completely differently