2017-09-11 19 views
0

の多型の関係を説明して、この接続を説明する方法を教えてください: enter image description hereはジャンゴ

をレコードの異なるタイプ(タイプ1、タイプ2またはタイプ3)があることができたテーブルがあります。接続を行うと、私は例Type2のために、種類を入力したいものを選択する方法

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' 

答えて

0

これは、行を除いて正しいよう:

name = GenericRelation(Product) 

は次のようになります。

name = GenericRelation(General) 

正しい逆の一般的な関係を形成するために。