1

我正在使用 Satchmo 框架开发 eshop。有谁知道我应该遵循哪些步骤才能根据自定义属性(材料类型)过滤产品,以便在页面(material.html)中呈现具有相同材料类型的产品?我应该创建一个 material_view 函数我应该覆盖 get_absolute_url 函数吗?

4

2 回答 2

1

如果你想在不触及核心代码的情况下做到这一点,我会在 models.py 中创建一个本地应用程序 localsite/product:

class Material(models.Model):
    product = models.ManyToManyField(Product, blank=True, null=True)
    name = models.CharField(_("Name"), max_length=30)
    slug = models.SlugField(_("Slug"), help_text=_("Used for URLs, auto-generated from name if blank"), blank=True, unique=True)
    description = models.TextField(_("Description"), blank=True, help_text="Optional")

将此新应用程序添加到您的管理员,并另外使它们从产品页面可用,将它们添加为内联:

# if you have lots of products, use the nice horizontal filter from django's admin
class MaterialAdmin(admin.ModelAdmin):
    filter_horizontal = ('product',)

class Material_Inline(admin.TabularInline):
    model = Material.product.through
    extra = 1 

admin.site.register(Material, MaterialAdmin)

# Add material to the inlines (needs: from product.admin import Product, ProductOptions)
ProductOptions.inlines.append(Material_Inline)
admin.site.unregister(Product)
admin.site.register(Product, ProductOptions)

然后您可以调整您的视图/网址:

# urls.py
url(r'^material-list/([\w-]+)/$', material_list, {}, name="material_list"),

# view.py
def material_list(request, slug):
    products = Product.objects.filter(material__slug='slug')
    return render_to_response('localsite/material/list.html', {'products':products}, context_instance=RequestContext(request))
于 2011-10-05T14:25:52.237 回答
0

当您说“自定义属性”时,您的意思是您已修改product.models.Product代码以添加另一个字段吗?

如果是这种情况,您可能需要创建自定义视图。

如果您的产品代码类似于...

class Product(models.Model):
    ...
    matieral_type = models.CharField(max_length=128)
    ...

...然后您可以构建这样的视图...

def material(request,material_type):
    prods = Product.objects.filter(material_type=material_type)
    return render_to_response('material.html',{'products',prods},RequestContext(request))
于 2011-03-14T05:17:17.023 回答