0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

-1

From the documentation :

Adding a foreign key from one of your own models to ContentType allows your model to effectively tie itself to another model class, as in the example of the Permission model above. But it’s possible to go one step further and use ContentType to enable truly generic (sometimes called “polymorphic”) relationships between models.

For example, it could be used in your model like so:

from django.contrib.contenttypes.models import ContentType
from django.db import models

class Event(models.Model):
    start_date = models.DateTimeField(null=True, blank=True)
    end_date = models.DateTimeField(null=True, blank=True)
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
Benbb96
  • 1,973
  • 1
  • 12
  • 21
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/27088261) – Brian Tompsett - 汤莱恩 Sep 04 '20 at 08:51
  • Thanks @BrianTompsett-汤莱恩 for pointing it me out, I've edited my answer. – Benbb96 Sep 04 '20 at 08:58