python - How do you catch this exception? -


This code is in the Django / DB / Models / Areas / makes an exception / defines it? class reversaling related object descriptor (six.we_metallacles (named object object descriptor method)): # This class provides functionality that provides related objects # Managers with attributes on model class Is available in the form, for which there is a single "remote" value for the category which defines the related field #for example "choice.poll", the pole attribute is a #reversing signal Layered object descriptor is example. Def __init __ (self, field_with_rel): self.field = field_with_rel self.cache_name = self.field.get_cache_name () @ cached_property def related object dots notexist (self): # Exception can not be made at the initial time because the # related model To be solved yet; `Rel.to` can still be the # string model reference, return type (str ('related object dot notexist'), (selffield, rlt.donotexist, attribute error), {})

This is in the Django / DB / Model / field / related. The PC raises the above exception:

  def __get __ (self, example, example_type = none): if Example is none: Returns the self attribute: rel_obj = getattr (e.g., self.cache_name) except AttributeError: val = self.field.get_local_related_value ( If any is not in Val: rel_obj = any other not: params = dict (rh_field.attname, getattr (for example, lh_field.attname)) for lh_field, self.field in rh_field. If Eaststens (extra_filters, dict ): Params.update (extra_filter) qs = qs.filter (** parameter) Other: qs = Qs.filter (extra_filter, ** params) # Imposes foreign key while assuming the database, it will not fail. Rel_obj = Qs.get () If not self.field.rel.multiple: setattr (rel_obj, self.field.related.get_cache_name (), example) setattr (example, self.cache_name, rel_obj) if rel_obj no one and not itself field.null: self Dhaan. No related objectNotClass ("% s has no% s."% (Self.field.model.__name__, self.field.name)) Other: Return rel_obj   

The problem is Try this code:

  Try: related to except val = getattr (obj, attr_name) Rawsingal Related Object Descriptor Addressed ObjectDoesNotExist: Val = None # Throwing Exceptions Causes   

isinstance (foo, related.FieldDoesNotExist) Foo except for exceptions: Foo type: Foo # Catch here, Not above

will not catch that exception

  & gt; & Gt; & Gt; Print Type (foo) & lt; Class 'django.db.models.fields.related.RelatedObjectDoesNotExist' & gt; Except related to   

and

  .related object dots notexist:   

one raises AttributeError: The 'module' object is any specialty 'related object dot notexist'.

  & gt; & Gt; isinstance (foo, related .reverseSingleRelatedObjectDescriptor.RelatedObjectDoesNotExist) traceback (most recent call final): File "& lt; string & gt;" , Line 1, & lt; Fragment & gt; TypeError: isinstance () arg 2 should be a class, type, or type of type and type   

which is probably the reason for this.

If your respective model is called Fu, then you can: < Except for pre> Foo.DoesNotExist:

Dango is amazing when it is not awesome related object dots notexist is a property that is dynamic on runtime Gives the type to be traced that type self.field.rel.to.DoesNotExist uses as a base class according to the Django document:

Exceptions DoesNotExist

DoesNotExist has been lifted when an object for a given parameter is not found for Django as a model of a model DoesNotExist provides an exception for which to identify the range of the object and leave you try / .

It is such a magic that does this. Once the model has been created, then self.field.rel.to.DoesNotExist is not present for that model is an exception.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -