Django admin save_model()中自我多对多字段没有保存

时间:2015-09-11 22:44:50

标签: python django-models django-admin

我有一个产品型号,其中more_colors自我多对很多字段,如果我要添加任何产品,并且该产品有更多颜色,那么它会自动添加到所有more_colors产品中,因为我使用的逻辑工作正常在python shell但是在django admin save_model()中,它没有保存,所以任何人都可以建议我做什么。我在写我的模型和管理员save_model()

class Product(models.Model):    
    site = models.ForeignKey(Site, verbose_name=_('Site'), default=1)
    name = models.CharField(_("Full Name"), max_length=255, blank=False,
        help_text=_("This is what the product will be called in the default site language.  To add non-default translations, use the Product Translation section below."), validators = [custom_unicode_validator])
    slug = models.SlugField(_("Slug Name"), blank=True,
        help_text=_("Used for URLs, auto-generated from name if blank"), max_length=255, validators = [custom_unicode_validator])
    sku = models.CharField(_("Code"), max_length=255, blank=True, null=True,
        help_text=_("Defaults to slug if left blank"), validators = [custom_unicode_validator])
    ....
    ....
    more_colors = models.ManyToManyField('self', blank=True, null=True, verbose_name=_('More Colors'), related_name='more_colors',limit_choices_to={'id__in':abc})
    ....
    ....  
class ProductOptions(admin.ModelAdmin):  
    def save_model(self, request, obj, form, change):  
        similar_sku_product=Product.objects.filter(sku__startswith=str(obj.sku)[0:7],active=1).exclude(sku=obj.sku)
        color_product=[]

        for similar_product in similar_sku_product:
            print similar_product.sku,obj.sku
            if len(similar_product.sku)==9:
                more_color_product.append(similar_product)
        for color_product in more_color_product:
            color_product.more_colors.add(obj)
            color_product.save()

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案我已经使用Admin表单自定义保​​存自己多个字段在这里我给代码。

class ProductForm(forms.ModelForm):
    class Meta:
        model =Product
    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)

        if self.instance and self.instance.pk:
            similar_sku_product=Product.objects.filter(sku__startswith=str(self.instance.sku)[0:7]).exclude(sku=self.instance.sku)
            more_color_product=[]
            for similar_product in similar_sku_product:
            #print similar_product.sku,obj.sku
                if len(similar_product.sku)==9:
                    more_color_product.append(similar_product)
                    print more_color_product
            for color_product in more_color_product:
                #print "products==>",color_product
                #m=color_product.more_colors.all()
                #print m ,"available color variation"
                #print "adding this products",self.instance
                color_product.more_colors.add(self.instance)
                #print "after add ",color_product.more_colors.all()
            self.fields['more_colors'].initial = color_product.more_colors.all()

    def save(self, commit=True):
        print "check more color form"
        product = super(ProductForm, self).save(commit=False)

        if commit:
            print "default"

            product.save()

        if product.pk:
            print "manual"

            product.more_colors = self.cleaned_data['more_colors']
            self.save_m2m()
            print product.more_colors
        return product