Cannot get this ModelForm view to save to the db, what am I doing wrong please

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Cannot get this ModelForm view to save to the db, what am I doing wrong please

MikeKJ

Cannot seem to get .save() to write to the db

This is the model


class Customer(models.Model):
    email = models.EmailField()
    postcode = models.CharField(max_length=10)
    def __unicode__(self):
    return self.email
    def save(self):
        self.postcode=upper(self.postcode)
        super(Customer, self).save()
 

This is the view:


class RegisterForm(ModelForm): class Meta: model = Customer def register(request): form = RegisterForm(request.POST) if form.is_valid(): email = form.cleaned_data['email'] postcode = form.cleaned_data['postcode'] postcode = upper(postcode) try: user = Customer.objects.all().filter(email=email).filter(postcode=postcode)[:1] if user: error = "You have already registered this email address and postcode, please login." return render_to_response("customer/register_form.html", {'error': error, 'form': form, 'content': content,},context_instance=RequestContext(request)) except Customer.DoesNotExist: cust_obj = Customer(email=email, postcode=postcode) cust_obj.save() return HttpResponseRedirect('/registration-thankyou/')

Also tried

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

It isn't throwing any errors just not saving to the table

Cheers for any insight/help

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b55ba86d-65dd-4c33-9144-4b9a5cf15bcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

RE: Cannot get this ModelForm view to save to the db, what am I doing wrong please

Matthew Pava

It’s hard to tell with some of the spacing in your email.  Python cares deeply about spacing.

 

When initializing a form, I typically do it as so:

form = RegisterForm(request.POST or None)

 

Also, the save method on the model has several arguments that you are not passing around.

    def save(self, *args, **kwargs):
        self.postcode = self.postcode.upper()

        super(Customer, self).save(*args, **kwargs)

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of MikeKJ
Sent: Monday, December 10, 2018 10:41 AM
To: Django users
Subject: Cannot get this ModelForm view to save to the db, what am I doing wrong please

 

Cannot seem to get .save() to write to the db

This is the model

 

class Customer(models.Model):
    email = models.EmailField()
    postcode = models.CharField(max_length=10)
    def __unicode__(self):
    return self.email
    def save(self):
        self.postcode=upper(self.postcode)
        super(Customer, self).save()
 

This is the view:


class RegisterForm(ModelForm):
    class Meta:
        model = Customer
 
def register(request):
    form = RegisterForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            postcode = form.cleaned_data['postcode']
            postcode = upper(postcode)
            try:
                user = Customer.objects.all().filter(email=email).filter(postcode=postcode)[:1]
                if user:
                    error = "You have already registered this email address and postcode, please login."
                    return render_to_response("customer/register_form.html", {'error': error, 'form': form, 'content': content,},context_instance=RequestContext(request))
            except Customer.DoesNotExist:
                cust_obj = Customer(email=email, postcode=postcode)
                cust_obj.save()
                return HttpResponseRedirect('/registration-thankyou/')

Also tried

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

It isn't throwing any errors just not saving to the table

Cheers for any insight/help

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b55ba86d-65dd-4c33-9144-4b9a5cf15bcc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/48cdf86ecf1240a99d940d4295ba0d34%40iss2.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.