例如,你的应用是app和app2,并且app中有A,B模型,而app2中有C,D模型。你只想查看app.A和app.B和app2.C
from django.db import modelsclass TaggedItem(models.Model): tag = models.SlugField() limit = models.Q(app_label = 'app', model = 'a') | models.Q(app_label = 'app', model = 'b') | models.Q(app_label = 'app2', model = 'c') content_type = models.ForeignKey(ContentType, limit_choices_to = limit) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey('content_type', 'object_id')在ForeignKey上使用limit_choices_to。
检查Django文档以获取详细信息和Q对象app_label。你需要编写适当的app_label和模型。这只是代码片段
加:我认为你写错了app_label。这可以为你提供帮助。
from django.contrib.contenttypes.models import ContentTypefor c in ContentType.objects.all(): print(c.app_label, c.model)



