AndrewIngram / django-extra-views

Django's class-based generic views are awesome, let's have more of them.
MIT License
1.39k stars 172 forks source link

Subclass ModelFormSetView #187

Closed shazianusrat closed 5 years ago

shazianusrat commented 5 years ago

I know this platform is not for posting such issues but humbly I request you to help me. I am working in django-oscar and I am trying to inherit a simple form but I ran into errors while subclassing ModelFormSetView from django-extra-views. Following is the code:

My Form: Link for BasketLineForm is https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/basket/forms.py#L14

from oscar.apps.basket.forms import BasketLineForm as CoreBasketLineForm

class AddBattery(CoreBasketLineForm):
    class Meta(CoreBasketForm.Meta):
          model=Battery
          fields = ('battery_manufacturer', 'battery_capacity', 'battery_warranty')

My Views: Link for BasketView is: https://github.com/django-oscar/django-oscar/blob/master/src/oscar/apps/basket/views.py#L32

from oscar.apps.basket.views import BasketView as CoreBasketView

class BasketView(CoreBasketView):

    def get_formset_kwargs(self):
        kwargs = super(BasketView, self).get_formset_kwargs()
        kwargs['strategy'] = self.request.strategy
        kwargs['battery']=self.request.modify
        return kwargs

    def get_queryset(self):
        return self.request.basket.all_lines()
    def get_battery_form(self):
        return AddBattery()
    def get_battery_objects(self):
        return Battery.objects.all()

    def get_context_data(self, **kwargs):
        context = super(BasketView, self).get_context_data(**kwargs)
        context['battery_form'] = AddBattery(self.request.POST)
        context['battery_data'] = self.get_battery_objects()
        return context

    def get_success_url(self):
        return safe_referrer(self.request, 'basket:summary')

I can't make my form work and can't display form based args/kwargs in django shell.

jonashaag commented 5 years ago

What are your errors? Please post tracebacks or elaborate on what doesn't work.

shazianusrat commented 5 years ago
Request Method: | GET
-- | --
http://localhost:8000/en/shop/basket/
1.11.15
TypeError
__init__() missing 1 required positional argument: 'strategy'
/home/shazia/oscar/getyoursolar/apps/oscar/basket/views.py in get_context_data, line 69
/home/shazia/oscar/bin/python
3.6.7
['/home/shazia/oscar/getyoursolar',  '/home/shazia/oscar/lib/python36.zip',  '/home/shazia/oscar/lib/python3.6',  '/home/shazia/oscar/lib/python3.6/lib-dynload',  '/usr/lib/python3.6',  '/home/shazia/oscar/lib/python3.6/site-packages']

On my line 69:

    def get_context_data(self, **kwargs):
        context = super(BasketView, self).get_context_data(**kwargs)
        context['battery_form'] = AddBattery() #this is line 69
        context['battery_data'] = self.get_battery_objects()
        print(context)
        return context
jonashaag commented 5 years ago

Sorry this looks like an issue that has nothing to do with extra views. Closing. Please ask on StackOverflow or get professional help.