Home > database >  Instance of djaform not updating
Instance of djaform not updating

Time:12-04

I have the next form in Django:

class Medical(forms.ModelForm):
    
    worker= forms.ModelChoiceField(
        queryset=Worker.objects.none(),
        empty_label=None,
        widget=forms.Select(attrs={'class': 'form-control'})
    )
    description=forms.CharField(
        widget=forms.Textarea(attrs={'class': 'form-control'})
    )
    upload=forms.FileField(
        widget=forms.FileInput(attrs={'class': 'form-control'})
    )
    
    class Meta:
        model = Medical_Issue
        fields = ( 
            'worker',
            'description',
            'upload',
        )

    def __init__(self, *args, **kwargs):
        user_id = kwargs.pop('user_id')
        method=kwargs.pop('method')
        super().__init__(*args, **kwargs)
        self.fields['worker'].queryset = Worker.objects.filter(user_id=user_id)

    def save(self, commit=True):
        m = super(Medical, self).save(commit=False)
        m.worker=self.cleaned_data['worker']
        m.description=self.cleaned_data['description']
        m.upload=self.cleaned_data['upload']
        if commit:
            m.save()
        return m

And following views:

def medical_list(request):
    worker=Worker.objects.filter(user_id=request.user.id).get()
    issues=Medical_Issue.objects.filter(worker=worker.id).order_by('-created_at')
    return render(request,'medical_list.html', {'medical_issues':issues})


def medical_add(request):
    print(request.user.id)
    if request.method == "POST":
        form = Medical(request.POST,request.FILES,user_id=request.user, method= 'ADD')
        if form.is_valid():
            form.save()
            return redirect('medical_list')
    else:
        form = Medical(user_id=request.user, method= 'ADD')
        return render(request, 'medical_add.html', {'method':'ADD','form': form})
    

def medical_edit(request,id_issue):
    worker=Worker.objects.get(user_id=request.user)
    issues=Medical_Issue.objects.filter(worker=worker).order_by('-created_at')
    
    issue= Medical_Issue.objects.get(id=id_issue)
    if request.method == 'GET':
        form = Medical(user_id=worker.user_id,instance=issue, method= 'EDIT')
    else:
        form = Medical(request.POST, request.FILES, user_id=worker.user_id, method= 'EDIT')
        if form.is_valid():
            form.save()
            return redirect('medical_list')
    return render(request,'medical_add.html', {'method':'EDIT','form': form})

Main probles is when adding, it saves record just fine, but when editing, it is creating a new instance of the issue. I´m trying to make it modifying save method on form, but maybe it´s not the right approach? With thata said, I have tried to add an id field to the form, but same results from this Thanks

CodePudding user response:

I suspect that you don't pass the correct instance value to the form in your medical_edit view:

    issue= Medical_Issue.objects.get(id=id_issue)
    if request.method == 'GET':
        form = Medical(
            user_id=worker.user_id,
            instance=issue, # <-- issue or parte?
            method='EDIT'
        )

UPDATE It should also include the instance argument in the POST method.


    if request.method == 'GET':
        # ...
    else:
        form = Medical(
            request.POST,
            request.FILES, 
            instance=issue, # <--
            user_id=worker.user_id, 
            method= 'EDIT'
        )
  • Related