Search code examples
django-formsdjango-oscardjango-formwizard

Django-Oscar Basket customization


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.

Following is the code:

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.


Solution

    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.