I have created a method that generates a new class and adds some methods to the class, but a strange bug Images and images: '' '' images '' SortedDict () generates images for image_name in image_fields, and I'm not sure what is happening: Moves a list of image_fields to '[image_name] = forms.ImageField (= false) required new_form = type (' ListingImagesForm2 ', (forms.BaseForm,), {' base_fields': images}). Image_fields add validation methods for the category for image_name #now: Print is "Image Name:", image_name setattr (new_form, 'clean_' + image_name, lambda auto: self._clean_photo (image_name)) #Add class setattr to _clean_photo This method is my method, which takes the list of images_fields (I'm creating a site in Django), and this is a whole bunch of methods (new_form, '_clean_photo', _clean_photo) new_form Creates the ImageField field and creates a class listingImagesForm2, and provides the image areas for the class. The problem is in making the methods, and more specifically the method material. In the loop: The signatures of the methods are created properly (like clean_pic_1, clean_pic_2 ...), but I think there is a problem in Lambda expression, as _clean_photo The method is always called with the same image name (which is the last image name in the image_fields list). Is there any good way to create dynamic method content (code) than lambda expressions? And why would my Lambda expression pass the last image_name to _clean_photo only for the loop?
for image_name in image_fields: Print is "Image Name:", image_name setattr (new_form, 'clean_' + image_name, lambda auto: self. _clean_photo (image_name))
The Python code behaves in such a manner as defined for the scope of the methods. Instead, use it: image_fields for image_name
Print: Print is "Image Name:", image_name setattr (new_form, 'clean_' + image_name, lambda auto, iname = image_name : Self._clean_photo (iname))
Using default keyword logic remembers Python when it is at the time of its phone (when it always takes the previous image) at the time of creating the lambda function .
Comments
Post a Comment