Override a dashboard form.

1,501 views
Skip to first unread message

Noel Martin Llevares

unread,
Dec 26, 2014, 5:59:21 PM12/26/14
to django...@googlegroups.com
I have a very simple requirement to display a custom Product field I have in the dashboard's product detail view.

My custom field shows up and functions properly in the Django admin but it is not visible in Oscar's Product Detail view (/dashboard/catalogue/products/01).

I tried using the same techniques in customizing a model but I am not successful.

I tried looking into the core code and my requirement can simply be accomplished by doing the following edit:

--- a/apps/dashboard/catalogue/forms.py
+++ b/apps/dashboard/catalogue/forms.py
@@ -264,7 +264,7 @@ class ProductForm(forms.ModelForm):
     class Meta:
         model = Product
         fields = [
-            'title', 'upc', 'description', 'is_discountable', 'structure']
+            'title', 'upc', 'description', 'is_discountable', 'structure', 'product_file']
         widgets = {
             'structure': forms.HiddenInput()
         }

I edited Oscar's core code in my virtualenv's site-packages directory. Obviously, this is not ideal. But I do not know how to cleanly override it.
Message has been deleted

Joshua Wedekind

unread,
Jan 1, 2015, 12:30:39 AM1/1/15
to django...@googlegroups.com
With the way Oscar product forms split fields between parent and child products, you have to explicitly state fields to be placed in a parent/child product form.

If you fork dashboard.catalogue, then you can overwrite the forms.ProductForm Class. Here is an example:

# my_project/apps/dashboard/catalogue/forms.py
from oscar.apps.dashboard.catalogue.forms import ProductForm as CoreProductForm

class ProductForm(CoreProductForm):
    
    class Meta(CoreProductForm.Meta):
        fields = [
            'title', 'upc', 'description', 'is_discountable',
            'structure', 'product_file']
    
    def delete_non_child_fields(self):
        """
        Deletes any fields not needed for child products. Override this if
        you want to e.g. keep the description field.
        """
        for field_name in ['description', 'is_discountable', 'product_file']:
            if field_name in self.fields:
                del self.fields[field_name]




Noel Martin Llevares

unread,
Jan 8, 2015, 5:29:02 PM1/8/15
to django...@googlegroups.com
Thank you for this reply. However, it still doesn't work for me. It's probably missing something that would tell Django to use my modified catalogue.forms instead of oscar.apps.dashboard.catalogue.forms but I still couldn't figure what that would be.

Noel

Joshua Wedekind

unread,
Jan 12, 2015, 9:45:12 PM1/12/15
to django...@googlegroups.com
You do need to add your app to installed apps in settings.py

from oscar import get_core_apps

INSTALLED_APPS
= [
   
...
] + get_core_apps(['oscarmod.catalogue', 'oscarmod.dashboard.catalogue',])

Joshua Wedekind

unread,
Jan 12, 2015, 9:46:41 PM1/12/15
to django...@googlegroups.com
Forgot to mention, I use "oscarmod" for the folder containing my Oscar app overrides, but I believe most people use "app" or "apps," so modify accordingly.

Noel Martin Llevares

unread,
Jan 14, 2015, 2:44:26 PM1/14/15
to django...@googlegroups.com
Thanks. That solved it. I added apps.dashboard.catalogue to get_core_apps().

--
https://github.com/tangentlabs/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
---
You received this message because you are subscribed to a topic in the Google Groups "django-oscar" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-oscar/i1dy43J5mww/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-oscar...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-oscar.
To view this discussion on the web, visit https://groups.google.com/d/msgid/django-oscar/27f31814-a262-4a35-9a0a-fd05c5771fc3%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

python test

unread,
Dec 21, 2017, 7:55:09 AM12/21/17
to django-oscar
i got this when i did the above
RuntimeError: Conflicting 'category' models in application 'catalogue': <class 'oscar.apps.catalogue.models.Category'> and <class 'catalogue.models.Category'>.

Marutesh Maru

unread,
Jun 5, 2018, 4:40:21 AM6/5/18
to django-oscar
Hi i want to override a form in basket app, I followed all the steps specified here but it is not overriding. My code is shown below.
please let me know what is the problem

from oscar.apps.basket.forms import AddToBasketForm as CoreAddToBasketForm
from django import forms
from oscar.forms import widgets
from django.utils.translation import ugettext_lazy as _

class AddToBasketForm(CoreAddToBasketForm):
    appointment_date = forms.CharField(max_length=128, label=_('Book an Appointment'),widget=forms.TextInput(attrs={'placeholder': 'DOB (DD/MM/YYYY)'}))

settings.py
[
...
'ecomm_models',
    'ckeditor',
] + get_core_apps(['forked_apps.basket'])

Samir Shah

unread,
Jun 5, 2018, 10:37:40 PM6/5/18
to django-oscar
Where have you placed this code? Is it in `forked_apps/basket/forms.py`?

Also note that by default the basket_form template tag will render SimpleAddToBasketForm and not AddToBasketForm - in which case overriding the latter will have no effect.

Marutesh Maru

unread,
Jun 6, 2018, 2:37:34 AM6/6/18
to django-oscar
Hi Samir
I have place the code in forked_apps/basket/forms.py .

You have said that SimpleAddToBasketForm would be rendered not AddToBasketForm then how can i achieve it please let me know. 

Samir Shah

unread,
Jun 6, 2018, 2:44:32 AM6/6/18
to django-oscar
You just need to override SimpleAddToBasketForm  instead of AddToBasketForm.

Marutesh Maru

unread,
Jun 6, 2018, 3:02:34 AM6/6/18
to django-oscar
class SimpleAddToBasketForm(AddToBasketForm):
    """
    Simplified version of the add to basket form where the quantity is
    defaulted to 1 and rendered in a hidden widget

    Most of the time, you won't need to override this class. Just change
    AddToBasketForm to change behaviour in both forms at once.
    """

    def __init__(self, *args, **kwargs):
        super(SimpleAddToBasketForm, self).__init__(*args, **kwargs)
        if 'quantity' in self.fields:
            self.fields['quantity'].initial = 1
            self.fields['quantity'].widget = forms.HiddenInput()

But in the comment section it is said that if the we override AddToBasketForm is enough.

Samir Shah

unread,
Jun 6, 2018, 3:06:06 AM6/6/18
to django-oscar
That comment is wrong, or intended for Oscar's internal use only (I'm not sure which - will look into it later) - it's not possible for the SimpleAddToBasketForm to see the changes you have made in your overridden form class, because it's not subclassing it. You need to override both forms.
Reply all
Reply to author
Forward
0 new messages