I have error like Type Error save() takes at least 2 arguments (1 given) in Django project.

153 views
Skip to first unread message

Remaze Vs

unread,
Aug 31, 2015, 6:10:10 AM8/31/15
to Django users
view.py file


class DealsForm(ModelForm):
    class Meta:
        model = Product
        fields = ['title','description','category','price','sale_price','slug','active','update_defaults','user']
        exclude = ('user',)


model.py file


class Product(models.Model):
    title = models.CharField(max_length=120)
    description = models.TextField(null=True, blank=True,max_length=200)
    category = models.ManyToManyField(Category, null=True, blank=True)
    price = models.DecimalField(decimal_places=2, max_digits=100, default=29.99)
    sale_price = models.DecimalField(decimal_places=2, max_digits=100,\
                                            null=True, blank=True)
    slug = models.SlugField(unique=True)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)
    update_defaults = models.BooleanField(default=False)
    user = models.ForeignKey(User)   

    def __unicode__(self):
        return self.title

    class Meta:
        unique_together = ('title', 'slug')

    def get_price(self):
        return self.price

    def get_absolute_url(self):
        return reverse("single_product", kwargs={"slug": self.slug})

    def save(self, request, *args, **kwargs):
        obj = super(DealsForm, self).save(commit=False, *args, **kwargs)
        obj.user = request.user 
        obj.save() 

Can you solve this problems ?  

Markus Holtermann

unread,
Aug 31, 2015, 6:42:52 AM8/31/15
to django...@googlegroups.com
Why "DealsForm"??

Also: you might want to share the traceback in the full future.

> obj.user = request.user
> obj.save()
>
>
>Can you solve this problems ?

/Markus

>
>--
>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 django-users...@googlegroups.com.
>To post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c82101d3-992c-4aaa-b932-787cd417009f%40googlegroups.com.
>For more options, visit https://groups.google.com/d/optout.


--

Florian Schweikert

unread,
Aug 31, 2015, 7:10:10 AM8/31/15
to django...@googlegroups.com
On 31/08/15 12:10, Remaze Vs wrote:
> def save(self,request,*args,**kwargs):

You defined save to take a second positional argument, so it's no wonder
it crashes if you call .save()
If you need the request in the save method you have to pass it,
otherwise remove the request parameter.

See docs for .save() overwrite:
https://docs.djangoproject.com/en/1.8/ref/models/instances/#django.db.models.Model.save

regards,
Florian

signature.asc
Reply all
Reply to author
Forward
0 new messages