Вот мой код:
class Product(models.Model) :
name = models.CharField(max_length=150)
price = models.DecimalField(max_digits=15, decimal_places=10)
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True)
date = models.DateField(auto_now=True, auto_now_add=False)
collection = models.ForeignKey(Collection, on_delete=models.CASCADE, blank=True, null=True)
slug = models.SlugField(max_length=100)
is_featured = models.BooleanField(default=False)
descripition = models.TextField()
is_published = models.BooleanField(default=True)
attributes = models.ManyToManyField('Attribute')
def __str__(self) :
return self.name
class Meta :
ordering = ("-date",)
class ProductVariant(models.Model) :
sku = models.CharField(max_length=120, unique=True)
name = models.CharField(max_length=120, blank=False, null=False)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
for attri in product.attributes :
attri.name = models.ForeignKey(AttributeVariant, on_delete=models.CASCADE, limit_choices_to={'attribute.name': attri.name})
class Meta :
ordering = ("name",)
def __str__(self) :
return self.product.name + self.name
class Attribute(models.Model) :
name = models.CharField(max_length=120)
def __str__(self) :
return self.name
class Meta :
ordering = ("name",)
class AttributeVariant(models.Model) :
name = models.CharField(max_length=120)
attribute = models.ForeignKey(Attribute, on_delete=models.CASCADE, related_name='values')
class Meta :
ordering = ("name",)
def __str__(self) :
return self.name
Что я пытаюсь сделать:
В классе ProductVariant я пытаюсь просмотреть атрибуты продукта и назначить каждому из них поле ForeignKey для AttributeVariants, чтобы получить значения этого конкретного атрибута. Это соответствует коду ниже:
for attri in product.attributes :
attri.name = models.ForeignKey(AttributeVariant, on_delete=models.CASCADE, limit_choices_to={'attribute.name': attri.name})
Однако, когда я пытаюсь запустить python manage.py makemigrations , я получаю
for attri in product.attributes :
AttributeError: 'ForeignKey' object has no attribute 'attributes'
Пожалуйста, дайте мне знать, если вы найдете решение этой проблемы.
Спасибо!