from django.views.generic import CreateView
from contract.models import AssertRequest
from contract.forms import AssertRequestForm
from customer.utils import get_current_customer
class AssertRequestCreateView(CreateView):
form_class = AssertRequestForm
# ...
def get_initial(self):
initial = super(AssertRequestCreateView, self).get_initial()
if self.customer:
initial['customer'] = self.customer
return initial
def dispatch(self, *args, **kwargs):
self.customer = get_current_customer(self.request)
return super(AssertRequestCreateView, self).dispatch(*args, **kwargs)
def get_form(self, form_class):
form = form_class(**self.get_form_kwargs())
if self.customer:
# make a field readonly
form.fields['customer'].widget.attrs['disabled'] = True
# limot choices to one value
form.fields['customer'].choices = [(self.customer.id, self.customer),]
return form
from django import forms
from customer.models import Customer
class AssertRequestForm(forms.ModelForm):
customer = forms.ModelChoiceField(
label=u'Cliente',
queryset=Customer.objects.all(),
required=False,
)
# ...