Django-Oscar Basket定制 [英] Django-Oscar Basket customization

查看:120
本文介绍了Django-Oscar Basket定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Djnago-oscar用于基于太阳能设备的电子商务网站.我需要使用电池名称",注释"和电池名称"为购物篮"模型添加一个选项 制造商".我不想将其添加到AbstractProduct子类中是有原因的.而是我想用子类Basket模型来构建它. 现在,我需要帮助来了解我的工作流程,以使其能够使用AddToBasket表单.在Django-oscar/basket中,有使用formset factory和form的formets.py. 我有些困惑,决定从社区寻求帮助.

I am using Djnago-oscar for Solar energy equipment based eCommerce site. I need to add an option to "Basket" model with "Battery Name", "Notes" and "Manufacturer". There is a reason I don't want to add it in subclass AbstractProduct. Rather I want to built it with subclass Basket model. Now I need help to understand my workflow to make this work with AddToBasket form. In Django-oscar/basket there are formsets.py using formset factory and a form. I am a bit confused and decided to get help from community.

以下是代码:

models.py

MANUFACTURERS = (
    ('UPS SYSTEMS', 'UPS SYSTEMS'),
    ('VOLTA', 'VOLTA'),
    ('TOSHIBA ', 'TOSHIBA '),
)

BATTERIES = (
    ('LITHIUM', 'LITHIUM'),
    ('NICAD', 'NICAD'),
    ('NIFE ', 'NIFE '),
)

class AddBattery(AbstractBasket):
    battery_name = models.CharField(max_length=1, choices=BATTERIES)
    manufacturers = models.CharField(max_length=1, choices=MANUFACTURERS)
    price = models.DecimalField(decimal_places=2, max_digits=6)
    notes = models.CharField(max_length=200, null=True, blank=True)

    def __str__(self):
        return self.battery_name


class Basket(AbstractBasket):
    add_bat=models.ForeignKey(_(u'Add a Batter'), to=AddBattery, null=True, blank=True)

forms.py

from django import forms
from django.conf import settings
from django.db.models import Sum
from django.utils.translation import ugettext_lazy as _

from oscar.forms import widgets
from oscar.apps.basket.forms import BasketLineForm as CoreBasketLineForm,\
SavedLineForm as CoreSavedLineForm, BasketVoucherForm as CoreBasketVoucherForm,\
AddToBasketForm as CoreAddToBasketForm

from .models import AddBattery
from oscar.core.loading import get_model, get_classes

Line = get_model('basket', 'line')
Basket = get_model('basket', 'basket')
Product = get_model('catalogue', 'product')



class BasketLineForm(CoreBasketLineForm):
    class AddBatteryForm(CoreBasketLineForm.Meta):
        model = AddBattery
        fields = ['battery_name', 'manufacturers', 'comment']

views.py

我需要帮助弄清楚这部分,因为它有太多的嵌套元素,我无法正确理解. 非常感谢您的帮助.

I need help to figure this part because it has so much nested elements and I couldn't get it right. Help is much appreciated.

模板: 我可以解决这个问题,因为我需要管理员才能添加它,但最终用户只能选择价格. 根据客户的选择,我需要将产品和电池的价格统一起来.这部分的任何建议也将很好 关于如何从购物车中的两者中获取合并价格以进行结帐.

Templates: I can work this out because I need admin to have ability to add it but end user only will have an option to select from with price. Upon selection by client I need to have consolidated price with products and plus the battery. Any advise for this part would be great as well about how to get consolidated price from both in cart for checkout.

推荐答案

  1. 请勿在购物篮"模型中添加您的字段.您需要替代Line模型.
  2. 子类forms.py和formsets.py.您只需要更改BasketLineForm,SavedLineForm和AddBasketForm.在以自己的形式将这些子类化之后.保留其余的表格.
  3. 您自己的表单集中的
  4. 子类BaseBasketLineFormSet和BaseSavedLineFormSet可根据需要进行编辑.
  5. 通过提供您添加的表单,表单集和args/kwargs来对BasketView和AddBasketView进行子类化.
  6. 将模板目录从应用程序复制到您自己的文件夹中,然后如上文作为最后一部分所述将表单添加到basket_total.html.
  1. Don't add your fields in Basket model. You need to subclass Line model instead.
  2. Subclass forms.py and formsets.py. You only need to change BasketLineForm, SavedLineForm and AddBasketForm. After you subclass these in your own forms. Leave rest of the Forms.
  3. Subclass BaseBasketLineFormSet and BaseSavedLineFormSet in your own formsets edit as per your need.
  4. Subclass BasketView and AddBasketView by supplying the forms, formsets and args/kwargs you added.
  5. Copy template directory from app to your own folder and add form at basket_total.html as you mentioned above as the last part.

但是,正如其他人所解释的那样,这违背了工作流程.就编程而言,在任何情况下都没有任何限制.但是,您应该始终考虑解决问题的最合理途径.

But having said that...its against the workflow as explained by others. There is no limitation at all for you in any case as far as programming is concerned. But you should always consider the most reasonable path to solve your problems.

这篇关于Django-Oscar Basket定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆