'self' is not define

17 views
Skip to first unread message

Remaze Vs

unread,
Aug 19, 2015, 7:25:20 AM8/19/15
to Django users
I am new in Django I am trying to insert session login username in database.but facing this error. 

my model.py file

class Product(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(null=True, blank=True)
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)
username=self.request.user.username


view.py

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


pls help

Florian Schweikert

unread,
Aug 19, 2015, 8:36:21 AM8/19/15
to django...@googlegroups.com
On 19/08/15 13:25, Remaze Vs wrote:
> username=self.request.user.username

What do you want to accomplish with this line?
This cannot work.
Where should self and self.request come from?

You probably want to us a ForeignKey to the User model.
(user = models.ForeignKey(User))
Afterwards you have to add request.user to the product in your view
where you save it.

Use something like
product = form.save(commit=False) # [0]
product.user = request.user
product.save()

And you should move your form to forms.py and add a view to views.py.

-- Florian

[0]
https://docs.djangoproject.com/en/1.8/topics/forms/modelforms/#the-save-method

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