Django:产品属性和自定义字段在产品页面中形成

时间:2014-08-16 13:30:20

标签: python django django-models django-forms django-views

我刚开始学习Django,我想创建一个包含属性,自定义字段和自定义字段选项的产品模型。自定义字段选项例如:
第1行:[你的文字] |自定义字段选项:[字体] [字体大小] [...]
第2行:[你的文字] |自定义字段选项:[字体] [字体大小] [...]

所以我创建了这个模型:

from django.db import models
from django.utils import timezone

class Product(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    sku = models.CharField(max_length=100)
    slug = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    active = models.BooleanField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    def __unicode__(self):
        return self.name

class ProductMeta(models.Model):
    product = models.OneToOneField('Product')
    title = models.CharField(max_length=100)
    description = models.TextField(max_length=250)

class ProductImage(models.Model):
    def upload_path(self, filename):
        return 'static/uploads/images/%s%s' % (timezone.now().strftime('%Y/%m/%d/%Y%m%d_'), filename)
    product = models.ForeignKey('Product')
    name = models.CharField(max_length=100)
    default = models.BooleanField()
    image = models.ImageField(upload_to=upload_path)
    def __unicode__(self):
        return self.name

class ProductCharacteristic(models.Model):
    product = models.ForeignKey('Product', related_name="characteristics")
    name = models.CharField(max_length=100)
    value = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class ProductAttribute(models.Model):
    category = models.ForeignKey('ProductAttributeCategory')
    products = models.ManyToManyField('Product', related_name="attributes")
    name = models.CharField(max_length=100)
    ordering = ['-category']
    def __unicode__(self):
        return u'%s : %s' % (self.category, self.name)

class ProductAttributeCategory(models.Model):
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class ProductAttributeValue(models.Model):
    attribute = models.ForeignKey('ProductAttribute', related_name="values")
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class ProductCustomField(models.Model):
    product = models.ForeignKey('Product', related_name="custom_fields")
    name = models.CharField(max_length=100)
    description = models.TextField(max_length=250)
    def __unicode__(self):
        return self.name

class ProductCustomFieldOption(models.Model):
    fields = models.ManyToManyField('ProductCustomField', related_name="options")
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

class ProductCustomFieldOptionValue(models.Model):
    option = models.ForeignKey('ProductCustomFieldOption', related_name="values")
    name = models.CharField(max_length=100)
    def __unicode__(self):
        return self.name

但现在我不知道如何在产品详细信息页面中创建表单,用户可以在其中选择产品属性(颜色,大小......)和产品自定义字段(以及自定义字段选项)。我尝试了很多但没有结果。

你能帮帮我吗? :)

1 个答案:

答案 0 :(得分:0)

你的问题我不清楚,你的问题更令人困惑。但是,如果它有帮助,请看这个

在models.py

from django.db import models
from model_utils import Choices

colour_choices = ('Blue', 'Green')


class Product(models.Model):
    name = models.CharField(max_length=100)

    def __unicode__(self):
        reuturn self.name


class ProductAttributes(models.Model):
    product = models.Foreignkey(Product, related_name='products')
    colour = models.CharField(choices=Choices(*colour_choices))

在您的forms.py中     来自django进口表格     来自.models import Product,ProductAttributes

class ProductForm(forms.ModelForm):

    class Meta:
        model = Product


class ProdductAttributesForm(forms.ModelForm):

    class Meta:
        model = ProductAttributes

相应地编写views.py,urls.py和模板 此方法将为您提供一个输入产品的文本框和用于选择颜色的下拉列表。 希望它有所帮助!