Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
881 views
in Technique[技术] by (71.8m points)

django models - __init__() got multiple values for argument 'instance'

I'm trying to override form but am getting multitple value for argument 'instance' this is my form class

class MarketProductForm(forms.ModelForm):
    
    lists = FarmerRequest.objects.all()
    request = forms.ModelChoiceField(queryset=lists)
    
    class Meta:
        model = MarketProduct
        fields = '__all__'
        
    def __init__(self, instance=None, *args, **kwargs):
        super().__init__(instance=instance, *args, **kwargs)
        
        if instance:
            requests = MarketProduct.objects.all()
            request_list = []
            for req in requests:
                request_list.append(req.request.id)
            # lists = FarmerRequest.objects.filter(~Q(id__in=request_list))
            lists = FarmerRequest.objects.all()
            self.fields['request'].queryset = lists
            
        else:
            requests = MarketProduct.objects.all()
            request_list = []
            for req in requests:
                request_list.append(req.request.id)
            lists = FarmerRequest.objects.filter(~Q(id__in=request_list))
            self.fields['request'].queryset = lists

inside my admin.py

form = MarketProductForm
question from:https://stackoverflow.com/questions/65642633/init-got-multiple-values-for-argument-instance

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...