Skip to content

Instantly share code, notes, and snippets.

@leonardoo
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leonardoo/75a14e5c4b8da9da4d11 to your computer and use it in GitHub Desktop.
Save leonardoo/75a14e5c4b8da9da4d11 to your computer and use it in GitHub Desktop.
instance
class InstanceMixin(object):
def add_arguments(self):
return {'empresa':self.request.empresa}
def get_form_kwargs(self):
"""
Returns the keyword arguments for instanciating the form.
"""
kwargs = super(InstanceMixin, self).get_form_kwargs()
kwargs.update(self.add_arguments())
return kwargs
class CashCreateView(LoginMixin, InstanceMixin, CreateView):
model = models.CajaGeneral
template_name = "cash/cash/create.html"
success_url = reverse_lazy('caja_general_list')
def add_arguments(self):
if hasattr(self.request, 'store'):
return {"store": self.request.store}
else:
return {'empresa': self.request.empresa}
def get_form_class(self):
if hasattr(self.request, 'store'):
return forms.CajaGeneralStoreForm
else:
return forms.CajaGeneralForm
class CajaGeneralForm(forms.ModelForm):
# TODO: Define form fields here
class Meta:
model = models.CajaGeneral
fields = ("store", "fecha")
def __init__(self, *args, **kwargs):
empresa = kwargs.pop('empresa', None)
super(CajaGeneralForm, self).__init__(*args, **kwargs)
if empresa:
queryset = self.fields['store'].queryset.filter(empresa=empresa)
self.fields['store'].queryset = queryset
class CajaGeneralStoreForm(forms.ModelForm):
# TODO: Define form fields here
def __init__(self, *args, **kwargs):
store = kwargs.pop('store', None)
super(CajaGeneralStoreForm, self).__init__(*args, **kwargs)
if store:
self.instance.store = store
class Meta:
model = models.CajaGeneral
fields = ("fecha", )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment