python - When django model access DB -


I'm curious how the DJ model works.

This means that when Model Access DB

for example we are this model person, and DB first_name = "abc"

  class person (models.Model) on record: first_name = models.CharField (MAX_LENGTH = 30) last_name = models.CharField (MAX_LENGTH = 30)  

and

  = Person.object.get copy (First_name = "abc") # Does it have access to DB? First_name = per.first_name # Otherwise its DB has access? First_name2 = per.first_name #first_name is cache? Or to access DB again?  

I use a similar model to display:

My model class IC (models.Model):

  PartNo = models.CharField (MAX_LENGTH = 200, unique = false) # 型号 sups 名 = models.CharField (MAX_LENGTH = 500, empty = true, default = no) mfr = models.ForeignKey (Mfr)  

and you can do that, it will print all the SQL queries sent by Django.

  & gt; & Gt; & Gt; Import logging & gt; & Gt; & Gt; Logger = logging.getLogger ('django.db.backends') & gt; & Gt; & Gt; Logger.setLevel (logging. DEBUG) & gt; & Gt; & Gt; Logger.addHandler (logging.SstreamHandler ()) & gt; & Gt; & Gt; IC = select IC.objects.get (id = '21 ') (0,001) `icbase_ic`.`id`,` icbase_ic`.`partno`, `icbase_ic`.`mfr_id`,` icbase_ic`.`sups` From `ICBCIIC` where` ICBCIIC`` id` = 21; Args = (21,) & gt; & Gt; & Gt; Ic.partno u'880 '& gt; & Gt; & Gt; The model for you, obviously, is simply database access in the  Person.objects.get (first_name = "abc") . 

But if the field is in the ForeignKey ManyToManyField or other relative field, its database will be accessed.

  & gt; & Gt; & Gt; Select ic.mfr (0.030) `icbase_mfr`.`id`,` icbase_mfr`.`name`, `icbase_mfr`.`cn_name`,` icbase_mfr`.`url`, `icbase_mfr`.`description`,` icbase_mfr `.`productcount`,` icbase_mfr`.`parent_id`, `icbase_mfr`.`logo_path`,` `icbase_mfr`.`active` from icbase_mfr` where` icbase_mfr`.`id` = 99; Args = (99,) & lt; MFF: Keystone Electronics & gt;  

I'm sorry for my bad english.


Comments

Popular posts from this blog

Member with no value in F# -

java - Joda Time Interval Not returning what I expect -

c# - Showing a SelectedItem's Property -