Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django admin save_model()中未保存自多对多字段_Python_Django Models_Django Admin - Fatal编程技术网

Python Django admin save_model()中未保存自多对多字段

Python Django admin save_model()中未保存自多对多字段,python,django-models,django-admin,Python,Django Models,Django Admin,我有一个产品模型,它有更多的颜色自多对多字段,如果我要添加任何产品,并且该产品有更多的颜色,那么它会自动添加到所有更多的颜色产品中,因为我使用的逻辑在python shell中运行良好,但在django admin save_模型中,这不是保存,所以任何人都可以建议我做什么。在这里我写我的模型和管理员保存_模型 最后,我找到了解决方案,我使用了管理表单来定制保存自我多对多字段,我在这里给出代码 class ProductForm(forms.ModelForm): class Meta:

我有一个产品模型,它有更多的颜色自多对多字段,如果我要添加任何产品,并且该产品有更多的颜色,那么它会自动添加到所有更多的颜色产品中,因为我使用的逻辑在python shell中运行良好,但在django admin save_模型中,这不是保存,所以任何人都可以建议我做什么。在这里我写我的模型和管理员保存_模型


最后,我找到了解决方案,我使用了管理表单来定制保存自我多对多字段,我在这里给出代码

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
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