0

我有一个有点奇怪的场景。我正在使用可以通过我的物业管理软件访问的只读数据库。它们允许用户在软件中定义字段。但是,它们不会显示为数据库中的特定字段。该数据库有 2 个与它们相关的表propuserdefinedpropuserdefinedvalues. 包含有关字段的propuserdefined所有信息(id、名称、描述),propuserdiefinedvalues包含与属性关联的值。它有propiduserdefinedidvalue。我的问题是,我需要访问propuserdefinedvalueswhere的值propid等于propid我的 for property in properties 语句中的值和userdefinedid等于 49。我该怎么做?是否带有模板标签?

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

这是我的模型。

class Propuserdefined(models.Model):
    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.IntegerField()
    propid = models.IntegerField()
    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'

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)
    altname = Propuserdefinedvalues.objects.filter(userdefinedid=49)
    class Meta:
        db_table = 'property'
4

2 回答 2

0

按原样使用您的模型(没有定义 ForeignKey,因此您不能使用 ORM 来跟踪关系),您可以获得这样的详细信息(如果我正确理解了您的问题):

property = Property.objects.get(name='my_property_name') # or however you get the property
prop_user_defined_values = Propuserdefinedvalues.objects.filter(propid=property.id, userdefinedid=49)

但是,如果您更改模型的顺序,并且将某些字段键入ForiegnKey

class Property(models.Model):
    # ... rest truncated ...

class Propuserdefined(models.Model):
    # ... rest truncated ...

class Propuserdefinedvalues(models.Model):
    property = models.ForeignKey(Property, db_column='propid')
    userdefined = models.ForeignKey(Propuserdefined, db_column='userdefinedid')
    # ... rest truncated ...

这将使您可以执行以下操作:

Propuserdefinedvalues.objects.filter(userdefined__name='my_name', property__name='my_property')
# or:
my_property = Property.objects.get(name='my_property')
Propuserdefinedvalues.objects.filter(userdefined__userdefinedid=49, property=my_property)

我建议您在此处阅读有关 Django 模型的信息:https ://docs.djangoproject.com/en/1.5/topics/db/models/ - 即使您有预先存在的表,它们也很容易正确处理,只要正如你所知道的关系。

(免责声明:未经测试的代码!可能是错误;))

于 2013-08-07T22:40:35.603 回答
0

听起来您正试图在 Django 模板中执行此操作。您应该使用 Python 代码,因为 Django 模板不是为此而设计的。

表的 Django 模型也不会提供最好的接口来访问这些属性。相反,您应该在它们之上创建一些函数。或者,您可以编写跨两个表连接的原始 SQL。

于 2013-08-06T21:46:21.297 回答