I m using generic view to update object.
I m using form :
class VehiPrepaClientForm(ModelForm):
class Meta:
model = VehiPrepa
fields = ('date_dem_prepa','carburant','ty_carburant')
def clean(self):
cleaned_data = self.cleaned_data
data = self.cleaned_data['date_dem_prepa']
vehiprepa = get_object_or_404(VehiPrepa,pk=object_id)
if data :
vehiprepa.demande_prepa = 1
vehiprepa.save()
date_in = vehiprepa.date_in
delta = data - date_in
date_dem = date_in + delta
if delta.days < 2 :
raise forms.ValidationError("Veuillez entrez une date au dela du : %s" %(date_dem) )
if data.weekday()>= 5 :
raise forms.ValidationError("La date choisie n'est pas valide, cela tombe un weekend")
#test si date demande sup a date_in et si pas samedi, ni dimanche, et si pas plus 32 vehi ce jour la
return cleaned_data
I m using this generic view :
def vehiprepa_update (request, vehiprepa_id):
user= request.user
login = user.username
vehiprepa = get_object_or_404(VehiPrepa,pk=vehiprepa_id)
url_template_base = "base_client_washtest.html"
response = create_update.update_object(
request,
form_class = VehiPrepaClientForm,
object_id = vehiprepa_id,
post_save_redirect = "/washtest/vehiprepa_list/0/",
template_name = 'washtest/vehiprepa_update.html',
template_object_name = 'object',
extra_context = {"url_template_base":url_template_base,"login":login,"vehiprepa":vehiprepa},)
return response
But i have an error, because object_id on my form is not known.... How can i get my request object id to use in my form clean ?
Thx
EDIT : HEre the error :
global name 'object_id' is not defined
Your error comes from this line:
vehiprepa = get_object_or_404(VehiPrepa,pk=object_id)
In your model form, you don't have a object_id
.
To solve this problem, refer to the documentation; which states:
Also, a model form instance bound to a model object will contain a self.instance attribute that gives model form methods access to that specific model instance.
From that we understand that the bound instance is in self.instance
. So, in your model form you need
vehiprepa = self.instance
instead of the get_object_or_404
line.