jeudi 31 décembre 2020

Django Product object has no attribute author

hi i am not new to django but i can not find the error . may be a new pair of eyes can saw that i just import usertestmixin and wrote the function test_func() but it is not working i do not know why it gave me the error 'Product' object has no attribute 'author'

my models for product is:

class Product(models.Model):
title = models.CharField(max_length=110)
slug = models.SlugField(blank=True, unique=True)
price = models.DecimalField(decimal_places=2, max_digits=6)
discount_price=models.FloatField(blank=True, null=True)
size = models.CharField(choices=SIZE_CHOICES, max_length=20)
color = models.CharField(max_length=20, blank=True, null=True)
image = models.ImageField(upload_to=upload_image_path)
description = models.CharField(max_length=1000)
featured = models.BooleanField(default=False)
time_stamp = models.DateTimeField(auto_now_add=True)

and my models.py for user is:

class User(AbstractBaseUser):
email = models.EmailField(max_length=255 ,unique=True)
full_name = models.CharField(max_length=255, blank=True, null=True)
active = models.BooleanField(default=True)  #can login
staff = models.BooleanField(default=False)  #staff user non super user
admin = models.BooleanField(default=False)  #superuser
time_stamp = models.DateTimeField(auto_now_add=True)

and my views.py is:

class ProductUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Product
template_name = 'product-form.html'
fields = ['title', 'price' , 'size' , 'color', 'image' , 'description']
def form_valid(self, form):
    form.instance.author = self.request.user
    return super().form_valid(form)

def test_func(self):
    self.object = self.get_object()
    return self.request.user == self.object.author

it gave me the error:enter image description here




Aucun commentaire:

Enregistrer un commentaire