我有一个类对不同信息的同一个泛型类有多个引用:
class Resort(models.Model):
id = models.PositiveIntegerField(_('HapNr.'), primary_key=True)
symbol = models.CharField(_('Kurzzeichen'), max_length=3, blank=True)
short_description = GenericRelation('MultiLingualText',
verbose_name=_('Beschreibung (ML)'),
related_query_name='resortshortdescriptions')
long_description = GenericRelation('MultiLingualText',
verbose_name=_('Einleitung (ML)'),
related_query_name='resortlongdescription')
class MultiLingualText(models.Model):
language = models.ForeignKey(hmodels.LanguageCode, verbose_name=_('Sprache'))
valid_from = models.DateField(_('Gültig ab'), default=timezone.now)
text = models.TextField(_('Text'))
content_type = models.ForeignKey(ContentType, verbose_name=_('Typ'))
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()
atu_id = models.CharField(_('ATU Text Id'), max_length=12, editable=False, blank=True)
atu_context = models.CharField(_('ATU Kontext'), max_length=1, editable=False, blank=True)
当我需要使用 Django Admin 使用此类时,我有两个内联,每个内联都有一个查询集,为该关系选择正确的文本。这工作正常。
我尝试通过为每个关系使用单独的序列化程序和视图集来做类似的事情,但是当我检索一个度假村时,它仍然显示每个关系的所有文本。
class ResortSerializer(serializers.HyperlinkedModelSerializer):
short_description = MultiLingualTextSerializerRSD(many=True, read_only=True)
long_description = MultiLingualTextSerializerRLD(many=True, read_only=True)
class Meta:
model = Resort
class MultiLingualTextSerializerRSD(serializers.HyperlinkedModelSerializer):
language = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = MultiLingualText
class MultiLingualTextViewSetRSD(viewsets.ModelViewSet):
serializer_class = MultiLingualTextSerializerRSD
queryset = MultiLingualText.objects.exclude(atu_id='').order_by('resort', 'content_type', 'object_id',
'-valid_from')
class ResortViewSet(viewsets.ModelViewSet):
queryset = Resort.objects.all().order_by('id')
serializer_class = ResortSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('id', 'sihot_nr')
所以基本上我的问题是,如何为每组文本使用不同的查询集?或者这有可能吗?
正确实施(感谢@lucasnadalutti)
class ResortSerializer(serializers.HyperlinkedModelSerializer):
short_description = serializers.SerializerMethodField()
long_description = serializers.SerializerMethodField()
def get_short_description(self, obj):
qs = MultiLingualText.objects.exclude(atu_id='').order_by('-valid_from', 'language__code')
return MultiLingualTextSerializer(qs, many=True, read_only=True).data
def get_long_description(self, obj):
qs = MultiLingualText.objects.filter(atu_id='').order_by('-valid_from', 'language__code')
return MultiLingualTextSerializer(qs, many=True, read_only=True).data