我有模特
class Section(CommonFields):
class Meta:
verbose_name = u'Раздел'
verbose_name_plural = u'Разделы'
leaf = False
top_content = HTMLField(verbose_name=u'Блок контента сверху', blank=True, null=True)
bottom_content = HTMLField(verbose_name=u'Блок контента снизу', blank=True, null=True)
def get_min_price(self):
items_ids = self.tree.get().children.values_list('object_id', flat=True)
return Item.objects.filter(id__in=items_ids, show=True).aggregate(Min('price'))['price__min']
class Item(CommonFields):
class Meta:
verbose_name = u'Товар'
verbose_name_plural = u'Товары'
leaf = True
price = models.PositiveIntegerField(verbose_name=u'Цена', blank=True, null=True)
old_price = models.PositiveIntegerField(verbose_name=u'Старая цена', blank=True, null=True)
sale = models.BooleanField(verbose_name=u'Скидка', default=False)
hit = models.BooleanField(verbose_name=u'Хит продаж', default=False)
property = HTMLField(verbose_name=u'Характеристики', blank=True, null=True)
content = HTMLField(verbose_name=u'Контент', blank=True, null=True)
feature = models.ManyToManyField('FeatureValue', verbose_name=u'Характеристики товара', related_name='items', blank=True)
class Feature(models.Model):
class Meta:
verbose_name = u'Характреистика'
verbose_name_plural = u'Характеристики'
def __unicode__(self):
return self.name
name = models.CharField(max_length=255, verbose_name=u'Название')
section = models.ForeignKey('Section', on_delete=models.CASCADE, verbose_name=u'Раздел', default=None)
class FeatureValue(models.Model):
class Meta:
verbose_name = u'Значение характеристики'
verbose_name_plural = u'Значения характеристик'
def __unicode__(self):
return self.value
value = models.CharField(max_length=255, verbose_name=u'Значение')
feature = models.ForeignKey('Feature', verbose_name=u'Характеристика', default=None)
我只想在特征领域中看到与商品部分重合的那些特征。 我使用formfield_for_manytomany过滤对象 但这没有用/我已经阅读了Django 1.7.7的官方文档
在admin.py
中class ItemAdmin(CatalogItemBaseAdmin):
model = Item
prepopulated_fields = {'slug': ('name',)}
def get_form(self, request, obj=None, **kwargs):
kwargs['formfield_callback'] = partial(self.formfield_for_dbfield, request=request, obj=obj)
return super(ItemAdmin, self).get_form(request, obj, **kwargs)
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name != "feature":
kwargs.pop('obj', None)
return super(ItemAdmin, self).formfield_for_dbfield(db_field, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
item = kwargs.pop('obj', None)
if db_field.name == "feature" and item:
section = item.tree.get().parent
kwargs['queryset'] = FeatureValue.objects.filter(feature__section=section)
return super(ItemAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)
但是没有用。请帮助我