Hello, i have a simple goal, displaying a form, populate it, save it to the database. At the execption of a new validator to check the weight of the image sent.
First point i need to write my own manipulator which is pretty much a copy of the model, i only want to add a validator to one field (image) the other default one suite me well how do i achieve this wihtout repeating myself. After writting this manipulator, i realise i need a save() function what is it intended to do ?, i could't find a full exemple. Does it override the view method described in the documentation http://www.djangoproject.com/documentation/forms/ ? I read i also need to specify the id of the object how ?. Here are the code of my model recette.py, views.py and Manipulator.py i put some question allong the key points i dont get. http://django.pastebin.com/564111 and the website to have a look http://ozserver.no-ip.com:345/cefinban/recette/ajouter Method to look at are create_recette in views.py and Manipulator.py some key methods : -------------------------------------------------- views.py def create_recette(request): manipulator = RecetteManipulator() if request.POST: # If data was POSTed, we're trying to create a new Place. new_data = request.POST.copy() # Check for errors. errors = manipulator.get_validation_errors(new_data) if not errors: # No errors. This means we can save the data! manipulator.do_html2python(new_data) new_recette = manipulator.save(new_data) # Redirect to the object's "edit" page. Always use a redirect # after POST data, so that reloads don't accidently create # duplicate entires, and so users don't see the confusing # "Repost POST data?" alert box in their browsers. return HttpResponseRedirect("/cefinban/recette/%i/" % new_recette.id) else: # No POST, so we want a brand new form without any data or errors. errors = new_data = {} # Create the FormWrapper, template, context, response. form = formfields.FormWrapper(manipulator, new_data, errors) return render_to_response('recettes/recettes_form', {'form': form}) -------------------------------------------------- manipulator.py from django.core import formfields, validators from django.models.recettes import recettes DIFFICULTY_CHOICES = ( (1, 'Simple'), (2, 'Intermedaire'), (3, 'Difficile'), ) CATEGORY_CHOICES = ( (1, 'Entree'), (2, 'Plat principal - Poissons'), (3, 'Plat principal - Viandes'), (4, 'Plat principal - Legumes et feculents'), (5, 'Dessert'), (6, 'Boisson'), (7, 'Amuse Gueule'), (8, 'Autre'), ) class RecetteManipulator(formfields.Manipulator): def __init__(self): #Why do i need to overide all the fields to only add one validator ? self.fields = [ formfields.TextField(field_name="nom", length=30, maxlength=80, is_required=True), formfields.TextField(field_name="auteur", length=30, maxlength=30, is_required=True ), formfields.LargeTextField(field_name="ingredients", is_required=True), formfields.LargeTextField(field_name="etapes", is_required=True), formfields.LargeTextField(field_name="conseils"), formfields.PositiveIntegerField(field_name="dure"), formfields.PositiveSmallIntegerField(field_name="quantite"), formfields.PositiveIntegerField(field_name="prix"), formfields.ImageUploadField(field_name="image_file",validator_list=[self.isValideImageWeight]), formfields.SelectField(field_name="difficulte", choices=DIFFICULTY_CHOICES, is_required=True), formfields.SelectField(field_name="categorie", choices=CATEGORY_CHOICES, is_required=True), ] def isValideImageWeight(self, field_data, all_data): print field_data if len(field_data["content"] > 500000) : # 500,000 bytes (500kB) raise validators.ValidationError, "Le fichier ne dois pas depasser 500kb" #def save(self,data): # Object= recettes.get_object(pk=self.id) # for field in self.fields: # if field.field_name+'_id' in Object.__dict__: # actual_field_name=field.field_name+'_id' # else: # actual_field_name=field.field_name # Object.__setattr__(actual_field_name,data[field.field_name]) # Object.save() def save(self, new_data): p = recettes.Recette(nom=new_data['nom'], categorie=new_data['categorie'], auteur=new_data['auteur'], ingredients=new_data['ingredients'], etapes=new_data['etapes'], conseils=new_data['conseils'], dure=new_data['dure'], difficulte=new_data['difficulte'], quantite=new_data['quantite'], prix=new_data['prix'], image=new_data['image_file'] ) p.save() Thank you for your support, stuck on a basic recquiremetn. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---