Я не могу добавить изображения товара, он показывает ошибку атрибута - PullRequest
0 голосов
/ 29 апреля 2020
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User

# Create your models here.


class Product(models.Model):
    CONDITION_TYPE = (
        ("New", "New"),
        ("Used", "Used"),

    )

    name = models.CharField(max_length=50)
    category = models.ForeignKey(
        'Category', on_delete=models.SET_NULL, null=True)
    brand = models.ForeignKey(
        'Brand', on_delete=models.SET_NULL, blank=True, null=True)
    description = models.TextField()
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    condition = models.CharField(max_length=50, choices=CONDITION_TYPE)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    created = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.name


class Category(models.Model):
    category_name = models.CharField(max_length=50)
    image = models.ImageField(upload_to='product/', blank=True, null=True)

    def __str__(self):
        return self.category_name

    class Meta:
        verbose_name = 'category'
        verbose_name_plural = 'categories'


class Brand(models.Model):
    category_name = models.CharField(max_length=50)

    def __str__(self):
        return self.category_name

    class Meta:
        verbose_name = 'brand'
        verbose_name_plural = 'brands'


class ProductImages(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    product_image = models.ImageField(
        upload_to='categories/', null=True, blank=True)

    def __str__(self):
        return self.Product

    class Meta:
        verbose_name = 'product images'
        verbose_name_plural = 'product images'

Когда я добавляю изображения продуктов в разделе админ-панели, отображается ошибка атрибута:

AttributeError at /admin/product/productimages/add/
'ProductImages' object has no attribute 'Product'

Request Method: POST
Request URL:    http://127.0.0.1:8000/admin/product/productimages/add/
Django Version: 3.0.5
Exception Type: AttributeError
Exception Value:    
'ProductImages' object has no attribute 'Product'
Exception Location: /home/durga/Desktop/django_projects/olx_clone/src/product/models.py in __str__, line 59
Python Executable:  /home/durga/Desktop/django_projects/olx_clone/bin/python
Python Version: 3.7.5
Python Path:    
['/home/durga/Desktop/django_projects/olx_clone/src',
 '/usr/lib/python37.zip',
 '/usr/lib/python3.7',
 '/usr/lib/python3.7/lib-dynload',
 '/home/durga/Desktop/django_projects/olx_clone/lib/python3.7/site-packages']
Server time:    Wed, 29 Apr 2020 12:03:20 +0000
...