Y
Y
Yura Khlyan2015-11-10 17:25:10
Django
Yura Khlyan, 2015-11-10 17:25:10

How to add one field for product in admin panel?

Good day.
I'm developing a store on Django-Oscar and I need to enter the tax level for each product.
I first added a field tax_ratein the product itself:

from django.utils.translation import ugettext_lazy as _
from django.db import models
from oscar.apps.partner.abstract_models import AbstractStockRecord
from django.core.validators import MinValueValidator

class StockRecord(AbstractStockRecord):

    tax_rate = models.DecimalField(
    	_("Tax rate"), decimal_places=2, max_digits=12,
    	blank=True, null=True, validators=[MinValueValidator(0)])

from oscar.apps.partner.models import *

Now you need to add this field in the admin panel. This can be achieved if the Oscar core is slightly tweakedapps/dashboard/catalogue/forms.py:
class StockRecordForm(forms.ModelForm):

    def __init__(self, product_class, user, *args, **kwargs):
        # The user kwarg is not used by stock StockRecordForm. We pass it
        # anyway in case one wishes to customise the partner queryset
        self.user = user
        super(StockRecordForm, self).__init__(*args, **kwargs)

        # If not tracking stock, we hide the fields
        if not product_class.track_stock:
            del self.fields['num_in_stock']
            del self.fields['low_stock_threshold']
        else:
            self.fields['price_excl_tax'].required = True
            self.fields['num_in_stock'].required = True
            self.fields['tax_rate'].required = True # добавил

    class Meta:
        model = StockRecord
        fields = ['partner', 'partner_sku', 'price_currency', 'price_excl_tax', 'tax_rate', # добавил
                  'price_retail', 'cost_price',  'num_in_stock', 'low_stock_threshold',]

Everything works great. But it's not good to change the source.
Then I forked the application apps.dashboard.catalogue, then in it (local) I created a file forms.pyin which I added (already corrected), but it still does not work. Someone can help me? I will be very grateful for your help. P.S. sorry for my bad english. class StockRecordForm(forms.ModelForm)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oscar Django, 2015-11-11
@MAGistr_MTM

Then I forked the app

Forked right? Did you add it in settings?
from oscar import get_core_apps
INSTALLED_APPS += get_core_apps([
    'path.to.local.folder.dashboard.catalogue',
])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question