Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Forms Model Forms Edit an Instance

Rogier Leegwater
Rogier Leegwater
5,589 Points

i created a couple of modelforms that i would like the user to be able to update.

i created a couple of modelforms that i would like the user to be able to update but in my database it just add a new form instead of updating it my codes: models.py

class EZ_Schein_Oange(models.Model):

Teilnehmer_Nummer = models.IntegerField(blank=False, primary_key=True)
BESR_Nr = models.IntegerField(blank=True)

def __str__(self):
    return str(self.Teilnehmer_Nummer)
class Einzahlung_Fuer(models.Model):

Bank = models.CharField(max_length=120, blank=False)
Adresse = models.CharField(max_length=120, blank=False)

def __str__(self):
    return self.Bank
class Zugunsten_Von(models.Model):

Name = models.CharField(max_length=120, blank=False)
Zusatz = models.CharField(max_length=120, blank=False)
Adresse = models.CharField(max_length=120, blank = True)
PLZ = models.IntegerField(blank=True)
Ort = models.CharField(max_length=120, blank=True)

def __str__(self):
    return self.Name
forms.py

class EZ_Schein_OangeForm(forms.ModelForm):

Teilnehmer_Nummer = forms.CharField(label='Teilnehmer-Nummer', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'VESR Teilnehmernummer'}))
BESR_Nr = forms.CharField(label='BESR-Nr', required=False, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '6-10 Stelliges Kundennummer'}))

class Meta:
    model = EZ_Schein_Oange
    fields = [ 'Teilnehmer_Nummer','BESR_Nr' ]
class Einzahlung_FuerForm(forms.ModelForm):

Bank = forms.CharField(label='Bank', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Bank'}))
Adresse = forms.CharField(label='Adresse', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Adresse'}))

class Meta:
    model = Einzahlung_Fuer
    fields = [ 'Bank', 'Adresse']
class Zugunsten_VonForm(forms.ModelForm):

Name = forms.CharField(label='Name', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Name'}))
Zusatz = forms.CharField(label='Zusatz', required=False,  widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Zusatz'}))
Adresse = forms.CharField(label='Adresse', widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Adresse'}))
PLZ = forms.CharField(label='PLZ', widget=forms.TextInput(attrs={'class': 'form-id', 'placeholder': 'PLZ'}))
Ort = forms.CharField(label='Ort', widget=forms.TextInput(attrs={'class': 'form-id', 'placeholder': 'Ort'}))

class Meta:
    model = Zugunsten_Von
    fields = [ 'Name', 'Zusatz', 'Adresse', 'PLZ', 'Ort' ]
views.py

def bank_edit(request):
            instance = get_object_or_404(EZ_Schein_Oange)
            form = EZ_Schein_OangeForm(request.POST or None, instance=instance)
            form2= Einzahlung_FuerForm(request.POST or None, instance=instance)            
            form3 = Zugunsten_VonForm(request.POST or None, instance=instance)

            title = 'EZ-Schein orange (ESR)'
            title2 = 'Einzahlung fuer'
            title3 = 'Zugunsten von'

            bankverbindung_app = EZ_Schein_Oange.objects.all()
            bankverbindung_app2 = Einzahlung_Fuer.objects.all()
            bankverbindung_app3 = Zugunsten_Von.objects.all()

            context = {
                        "title": title,
                        "title2": title2,
                        "title3": title3,
                        "form2": form2,
                        "form3": form3,
                        "bankverbindung_app": bankverbindung_app,
                        "bankverbindung_app2": bankverbindung_app2,
                        "bankverbindung_app3": bankverbindung_app3,
                        "instance": instance,
                        "form": form,


            }
            #if request.method == 'POST':
            if form.is_valid():

                        form = EZ_Schein_OangeForm(instance=instance, data=request.POST)

                        instance = form.save(commit=False)
                        instance.save()

                        return render(request, 'bankverbindung.html', context)

                        print("error: form not valid")

            return render(request, 'bank_edit.html', context)

[markdown added by mod]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Rogier, the code is difficult to read as it is. Can you please clean up the indentation?

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

In the line

instance = get_object_or_404(EZ_Schein_Oange)

Where is the correct instance of EZ_Schein_Oange being accessed. You need to add selection arguments (such as a pk value passed in through the URL). Without the referencing the correct instance, a new instance will be created.

The other two forms do not have valid instances, but regardless those form ps do not have code to save them.