python - Why does len() execute __set__ of my class? -
I have implemented a Django, it is an image field that allows a URL string to load an image In addition to specifying a file directly.
ImageFileDescriptor of imports Import models Import django.core.files.base import Django.db.models.fields.files from Uuid import urllib.request django.db UrlImageFileDescriptor (ImageFileDescriptor) : Def __set __ (self, example, value): # if string is used for assignment, then to fetch an image from URL # and store it on the server if isinstance (value, str): try: feedback = Urllib.request.urlopen (value) image = response.read () name = str (uuid.uuid4 ()) + '.png' value = ContentFile (image, name) Except: print ('error fetch', value) Suup Su passes () .__ __ set (eg, value) square UrlImageField (models.ImageField): descriptor_class = UrlImageFileDescriptor
Normally, field work. But for some reasons, the DJ gives the string value itself internally. When is a query set of models using the field, __ set __
that is called with the string except print statements in Clause Upload / / 50e170bf-61b6- Error fetching 4670 - 90d1-0369a8f9bdb4.png
.
I can reduce calls by 1.7c1.
def mill (self, * args, ** Kwargs): "" performs the query and returns the single object matching the given keyword argument. " "Clone = self.philter (* args, ** kwargs) if self.query.can_filter (): clone = clone.order_by () clone = clone [: MAX_GET_RESULTS + 1] num = len (clone) # This line is my field Causes the setting if num == 1: return clone._result_cache [0] # ...
set the __ set of my area
why Running? I can validate the input value for a valid URL to work around it, but I want to know the first reason.
The story is yours To get the length of the query in the receiver:
get the file "C: \ repository \ virtualenv \ lib \ site-packages \ django \ db \ Model \ query.py" 350. Num = len
This brings all the query results to the list:
The file "C: \ repository \ virtualenv \ lib \ site- packages \ django \ Db \ models \ query.py "in __len__ 122. Self._fetch_all () file" C: \ repository \ virtualenv \ lib \ site-packages \ django \ db \ models \ query.py "_fetch_all in 966. Self._result_cache = list (self.iterator ())
For each query result, it creates a model object using data from DB:
< Code> file "C to create model object: \ repository \ virtualवेश \ lib \ site-package \ degenerate \ db \ model \ query.py" in iterator 275 Obj = Model (* row_data)
This sets each field in the model:
The file "C: \ repository \ virtualenv \ lib \ site -packages \ django \ db \ Model \ basis.py "in __init__ 383. setattr (self, field.adventon, or
set __ on your custom model field
Calling Winds:
File "C: \ repository \ invoicepad \ apps \ customer \ fields.py" in __set__ 18. response = urllib.request.urlopen (value)
It is difficult to say more about the reasons behind the large scale, because both of me I do not know much and because I do not know what your DB structure is like. However, it essentially shows that the database field that populates your UriImageField
contains data in it is not valid to apply your Varnlekk (for example, see your error, dB 'Upload / copy / 50e170bf-61b6-4670-90d1-0369a8f9bdb4.png'
but There really is no such file .)
Comments
Post a Comment