0
Sag mir, wie diese Verbindung beschreiben: die polymorphe Beziehung von Django
Es ist eine Tabelle, in denen es unterschiedliche Arten von Datensätzen (Typ1, Typ2 oder Typ3).
models.py
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
class General(models.Model):
field1 = models.CharField(max_length=512, blank=True, null=True)
field2 = models.CharField(max_length=512, blank=True, null=True)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
db_table = 'General'
class Type1(models.Model):
name = GenericRelation(Product)
address = models.CharField(max_length=512, blank=True, null=True)
number = models.CharField(max_length=256, blank=True, null=True)
class Meta:
db_table = 'Type1'
Wie eine Verbindung zu machen und entscheiden, was ich einen Typ eingeben möchten, zum Beispiel Typ2?