在上面的示例中,我有一个可以是两种(或更多)类型的模型类别。如果我在ContentA模型中使用limit_to_choice,如果我们之间有通用关系,我怎么能只使用类别类型“A”?
# Taxonomy models
from django.db import models
from mptt.models import MPTTModel
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Category(MPTTModel):
TYPE_CHOICES = (
('A', 'Type A'),
('B', 'Type B'),
)
name = models.CharField(max_length=128)
type = models.CharField(max_length=1, choices=TYPE_CHOICES)
parent = models.ForeignKey('self', null=True, blank=True, related_name='%(class)s_parent')
class MPTTMeta:
parent_attr = 'parent'
order_insertion_by = ['type', 'name', ]
class CategorizedItem(models.Model):
category = models.ForeignKey(Category)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class ContentA(models.Model):
categories = generic.GenericRelation(CategorizedItem, limit_choices_to={'category_type__exact': 'A'})
name = models.CharField(max_length=128)
class ContentB(models.Model):
categories = generic.GenericRelation(CategorizedItem, limit_choices_to={'category_type__exact': 'B'})
name = models.CharField(max_length=128)