python - The Django Way To Write This Query? -
i have obscure database in django. database read , created property managements software. in view need write query specific record.
select * propuserdefinedvalues propid = propid , userdefinedid = 49
is there django way execute instead of having raw sql? looping through "property" table. these records in "userdefinedcalues" table.
here models.
class property(models.model): propid = models.integerfield(primary_key=true) name = models.charfield(max_length=35l, blank=true) shortname = models.charfield(max_length=6l, blank=true) street1 = models.charfield(max_length=50l, blank=true) street2 = models.charfield(max_length=50l, blank=true) city = models.charfield(max_length=50l, blank=true) state = models.charfield(max_length=2l, blank=true) zip = models.charfield(max_length=50l, blank=true) phone = models.charfield(max_length=21l, blank=true) fax = models.charfield(max_length=50l, blank=true) email = models.charfield(max_length=255l, blank=true) manager = models.charfield(max_length=25l, blank=true) billname1 = models.charfield(max_length=35l, blank=true) billname2 = models.charfield(max_length=35l, blank=true) billstreet1 = models.charfield(max_length=50l, blank=true) billstreet2 = models.charfield(max_length=50l, blank=true) billcity = models.charfield(max_length=50l, blank=true) billstate = models.charfield(max_length=2l, blank=true) billzip = models.charfield(max_length=50l, blank=true) proptaxid = models.charfield(max_length=35l, blank=true) rentchargetype = models.charfield(max_length=20l, blank=true) lastpostdate = models.datefield(null=true, blank=true) lastweeklypostdate = models.datefield(null=true, blank=true) comments = models.charfield(max_length=25l, blank=true) enablespeciallatecharge = models.integerfield(null=true, blank=true) fixedlatecharge = models.integerfield(null=true, blank=true) fixedlateamount = models.floatfield(null=true, blank=true) fixedlaterentonly = models.integerfield(null=true, blank=true) percentlate = models.integerfield(null=true, blank=true) percentlateamount = models.floatfield(null=true, blank=true) percentlatefullcharge = models.integerfield(null=true, blank=true) percentlaterentonly = models.integerfield(null=true, blank=true) perdaylate = models.integerfield(null=true, blank=true) perdaylateamount = models.floatfield(null=true, blank=true) perdaylategrace = models.integerfield(null=true, blank=true) perdaylategracenum = models.integerfield(null=true, blank=true) perdatelatelimitamount = models.floatfield() perdaylategracenonretro = models.integerfield() perdaylategraceexclweekends = models.integerfield() perdaylategraceexclholidays = models.integerfield() datecreated = models.datetimefield(null=true, blank=true) updated = models.datetimefield(null=true, blank=true) userid = models.integerfield(null=true, blank=true) logofile = models.charfield(max_length=255l, blank=true) merchantid = models.charfield(max_length=255l, blank=true) epaybankid = models.integerfield() epaylimit = models.floatfield() epayenabled = models.integerfield() achconveniencefeeenabled = models.integerfield() ccconveniencefeeenabled = models.integerfield() rwaachconvenciencefeeenabled = models.integerfield() rwaccconveniencefeeenabled = models.integerfield() epayislimited = models.integerfield() epayusedefaults = models.integerfield() achconveniencefee = models.floatfield(null=true, blank=true) ccconveniencefee = models.floatfield(null=true, blank=true) rwaachconveniencefee = models.floatfield(null=true, blank=true) rwaccconveniencefee = models.floatfield(null=true, blank=true) epaychargetype = models.integerfield() epayamounttype = models.integerfield() epaysetamount = models.floatfield() epaycustlimit = models.floatfield() sqft = models.integerfield() lateminbalance = models.floatfield(null=true, blank=true) defaultbank = models.integerfield() postday = models.integerfield(null=true, blank=true) active = models.integerfield(null=true, blank=true) iscommercial = models.integerfield(null=true, blank=true) assignedissueuserid = models.integerfield(null=true, blank=true) class meta: db_table = 'property' class propuserdefined(models.model): id = models.integerfield(primary_key=true) userdefinedid = models.integerfield(primary_key=true) name = models.charfield(max_length=50l, blank=true) type = models.integerfield() userid = models.integerfield(null=true, blank=true) updated = models.datetimefield(null=true, blank=true) datecreated = models.datetimefield(null=true, blank=true) combolist = models.textfield(blank=true) class meta: db_table = 'propuserdefined' class propuserdefinedvalues(models.model): userdefinedid = models.foreignkey(propuserdefined) propid = models.foreignkey(property) value = models.textfield(blank=true) userid = models.integerfield(null=true, blank=true) updated = models.datetimefield(null=true, blank=true) datecreated = models.datetimefield(null=true, blank=true) class meta: db_table = 'propuserdefinedvalues'
here view
def properties(request): properties = property.objects.all().order_by('state') altname = select * propuserdefinedvalues propid = 73 , userdefinedid = 49 return render_to_response('properties/index.html', {'properties':properties, }, context_instance=requestcontext(request))
here template
{% property in properties %} {{ altname }}<br><br> {% endfor %}
thanks in advance help. brandon
ok, guesswork you're trying do, if you're trying template propuserdefinedvalues
predefined userdefinedid
each property in turn, way either define custom template tag or define method on property
class. tend towards latter, @ least when don't need fancy html. can add attributes model instances in view, find less elegant.
class property(models.model): # huge list of fields def altname(self): return self.propuserdefinedvalues_set.filter(userdefinedid__name='altname')
or whatever name appropriate - avoid hardcoding id if @ possible.
then in template:
{% property in properties %} {{ property.altname }}<br><br> {% endfor %}
in view, using select_related
in queryset preload join boost performance.
Comments
Post a Comment