2017-12-13 16 views
0
class A(models.Model): 
    relation = GenericRelation('B') 
    another_relation = GenericRelation('B') 

class B(models.Model): 
    content_type = models.ForeignKey(ContentType, blank=True, null=True) 
    object_id = models.PositiveIntegerField(blank=True, null=True) 
    content = GenericForeignKey('content_type', 'object_id') 

a = A() 
b = B() 

a.another_relation.all()の結果としてbを得るように接続を設定するにはどうすればよいですか?設定された一般的な関係を修正するにはどうすればいいですか?

答えて

0

Django documentation GenericRelationから、クラスを最初の引数として取ります。

class B(models.Model): 
    content_type = models.ForeignKey(ContentType, blank=True, null=True) 
    object_id = models.PositiveIntegerField(blank=True, null=True) 
    content = GenericForeignKey('content_type', 'object_id') 

class A(models.Model): 
    relation = GenericRelation(B) 
    another_relation = GenericRelation(B) 

a.another_relation.all()を使用できるはずです。

関連する問題