如果我知道第一个字段值,如何从模型中获取另一个字段值? [英] How to get another field value from model if I know first field value?
问题描述
我正在尝试学习django并在访问模型时遇到问题.
I am trying to learn django and faced problem accessing models.
我在 models.py
中有一个看起来像这样的模型:
I have model in models.py
that look like this:
class Countries(models.Model):
Country = models.CharField(max_length=200)
PPP = models.DecimalField(max_digits=15, decimal_places=10)
def __str__(self):
return self.Country
现在在 views.py
中,我想根据用户输入进行一些计算.用户将从第一个字段( Country
)中选择值,我需要根据该值找到第二个字段值( PPP
).
Now in views.py
I want to make some calculations based on user input. User will choose value from first field (Country
) and I need to find second field value (PPP
) based on that.
这意味着我的数据结构如下:
It means that I have data structured something like that:
Country | PPP
----------------
Lithuania | 0.45
Germany | 0.86
Estonia | 0.55
Spain | 0.77
因此,我将了解国家/地区,并且需要访问其PPP.我该怎么办?
So I will know country and I will need to access its PPP. How should I do it?
由于我尝试了各种解决方案,所以给了我一个错误.
Because various solutions I tried gives me an error.
我不满意的最新解决方案:
Lats solutions I tired:
pVal = str(form.cleaned_data['Country'])
country = Countries.objects.first()
pValKof = getattr(country, pVal)
根据我的研究,我觉得我应该在解决方案附近,但我只是无法完全理解 getattr
.也许还有其他方法可以实现这一目标?
Based on my research I feel I should be somewhere near the solution, but I just can't understand getattr
completely. Or maybe there is some other way to achieve this?
这是我的 forms.py
class PGPskaiciuokle(forms.Form):
country = forms.ModelChoiceField(queryset=Countries.objects.all(),
label="Choose country")
def clean_country(self):
country = self.cleaned_data['country']
return country
推荐答案
更改了我的解决方案,因为我错过了Form字段是ModelChoiceField的事实
Changed my solution, because I missed the fact that the Form's field was a ModelChoiceField
代替:
pVal = str(form.cleaned_data['Country'])
country = Countries.objects.first()
pValKof = getattr(country, pVal)
执行此操作:
country = form.cleaned_data['Country']
ppp = country.PPP
此外:
-
Python中的属性应以小写字母开头,类以大写字母开头,因此,使用Country和ppp代替Country和PPP(不确定ppp代表什么,使用更多的内容不会有什么害处描述性名称)
Properties in Python should start with a lowercase letter, classes with an uppercase letter, so instead of Country and PPP, use country and ppp (Not sure what ppp stands for, it wouldn't hurt to use a more descriptive name)
型号名称应为单数.因此,请使用国家代替
A model name should be singular. So instead of Countries, use Country
国家(地区)属性可能更具描述性.尝试使用名称代替国家/地区.
The property Country could be more descriptive. Try using name instead of country.
我会为每个国家/地区选择一个唯一的名称.
I would opt for a unique name for each country.
所以要稍微整理一下代码:
So to clean up your code a bit:
class Country(models.Model):
name = models.CharField(max_length=200, unique=True, null=False)
ppp = models.DecimalField(max_digits=15, decimal_places=10)
def __str__(self):
return self.name
还有
country = form.cleaned_data['country']
ppp = country.PPP
以及表单文件:
class PGPskaiciuokle(forms.Form):
country = forms.ModelChoiceField(queryset=Country.objects.all(),
label="Choose country")
这篇关于如果我知道第一个字段值,如何从模型中获取另一个字段值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!