-1

我在 Django 中有一个不起眼的数据库。该数据库是只读的,由我的物业管理软件创建。基本上在我看来,我需要编写此查询来获取特定记录。

SELECT * FROM propuserdefinedvalues WHERE propid = propid and userdefinedid = 49

有没有 Django 方法来执行它而不是使用原始 SQL?我正在遍历“属性”表。这些记录在“userdefinedcalues”表中。

这是我的模型。

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'

这是我的看法

def properties(request):
    properties = Property.objects.all().order_by('state')
    altname = SELECT * FROM propuserdefinedvalues WHERE propid = 73 and userdefinedid = 49
    return render_to_response('properties/index.html', {'properties':properties, }, context_instance=RequestContext(request))

这是我的模板

{% for property in properties %}
{{ altname }}<br><br>
{% endfor %}

在此先感谢您的帮助。布兰登

4

2 回答 2

1

好吧,这很简单。

var = 49
Property.objects.filter(propid=var).filter(userdefinedid=var)

那将是一种方法。

于 2013-08-07T21:59:43.803 回答
1

好的,这只是您要执行的操作的猜测,但是如果您尝试让模板依次查找Propuserdefinedvalues具有每个属性的预定义userdefinedid的 a,那么执行此操作的方法是定义一个自定义模板标签或在类上定义方法Property。我倾向于后者,至少当我不需要任何花哨的 HTML 时。您还可以在视图中为模型实例添加属性,但我发现这不太优雅。

class Property(models.Model):
    # huge list of fields

    def altname(self):
        return self.propuserdefinedvalues_set.filter(userdefinedid__name='altname')

或任何合适的名称 - 尽可能避免对 ID 进行硬编码。

然后在模板中:

{% for property in properties %}
    {{ property.altname }}<br><br>
{% endfor %}

在视图中,select_related在查询集中使用预加载连接可能会提高性能。

于 2013-08-07T22:08:26.690 回答