我可以在Django中为linkedItem的管理员制作一个filter_horizontal.现在,我希望通用关系的另一端的内容,比如一个名为Bakery的应用程序,能够使用成分进行filter_horizontal.
茶水
models.py
from django.db import modelsfrom django.contrib.ContentTypes.models import ContentTypefrom django.contrib.ContentTypes import fIEldsclass IngredIEnt(models.Model): ''' Model containing all the ingredIEnts,their slugs,and their descriptions ''' name = models.CharFIEld(unique=True,max_length=100) slug = models.SlugFIEld(unique=True,max_length=100) description = models.CharFIEld(max_length=300) # method to return the name of the db entry def __str__(self): return self.nameclass linkedItem(models.Model): ''' Model that links ingredIEnts to varIoUs other content models ''' content_type = models.ForeignKey(ContentType) object_ID = models.PositiveIntegerFIEld() content_object = fIElds.GenericForeignKey('content_type','object_ID') ingredIEnt = models.ManyToManyFIEld(IngredIEnt) # method to return the name of the db entry def __str__(self): return self.ingredIEnt.name # defines options for the model itself class Meta: unique_together = (('content_type','object_ID')) # prevents duplicates
面包店
admin.py
from django.contrib import adminfrom bakery.models import Cakeclass Cakeadmin(admin.Modeladmin): filter_horizontal = ('') # what to put here so ingredIEnts show up?
有任何想法吗?
解决方法 一个解决方案是为linkedItem创建一个 GenericTabularInline并对显示设置一些限制以避免重复,如下所示:from django.contrib.ContentTypes.admin import GenericTabularInlineclass linkedItemadmin(GenericTabularInline): model = linkedItem # choosing the fIEld and display fIEld = ['ingredIEnt'] filter_horizontal = ['ingredIEnt'] # These help with removing some potential issues in the admin extra = 0 min_num = 1 max_num = 1 can_delete = False
然后在Cakeadmin中,我可以做到这一点,使成分出现.
class Cakeadmin(admin.Modeladmin): inlines = [linkedItemadmin]@H_502_2@ 总结
以上是内存溢出为你收集整理的如何使用Django管理员的ContentTypes访问两个应用程序之间的ManyToManyField?全部内容,希望文章能够帮你解决如何使用Django管理员的ContentTypes访问两个应用程序之间的ManyToManyField?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)