django - TypeError save() takes at least 2 arguments (1 given) -
i have error type error save() takes @ least 2 arguments (1 given) in django project.
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 solve problems ?
it seems not passing request
object when calling .save()
. need pass request
object according definition of save()
.
also, please check .save()
function calling super()
dealsform
class argument in product
model class.
edit:
since want set user
on product
object, can following instead:
def my_view(request): ... product = my_deals_form.save(commit=false) # product instance product.user = request.user # set user on product product.save() # save object
you don't need override save()
method. call model form instance commit=false
argument, set user on product
object , save object.
Comments
Post a Comment