我不明白為什么我的原始查詢會將結果與用戶數量相乘。我使用的是INNER JOIN,我系結了兩個物件,所以我真的搞不懂。 (我對Django查詢語言沒有經驗,所以我使用的是原始查詢)
。views.py
def index(request):
projekt = Projekt.objects.raw('SELECT projekt_id, stressz_projekt.count, last_name, first_name, stressz_profile.id, stressz_profile. 在這個程序中,我們可以看到,在我們的網站上,有很多關于我們的資訊,例如:"我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站","我們的網站 "等。)
template = loader.get_template('stressz/index.html'/span>)
背景關系 = {
'service': projekt,
}
return HttpResponse(template.render(context, request))
如果我使用Django ORM,我會得到同樣的結果
profile = Profile.objects.get(user=request.user)
projekt = Profile.objects.filter(profile.procurement)
models.py
class Viselkedestipus(models. Model)。)
def __str__(self):
return str(self.user_name)
user_name = models.ForeignKey(User, on_delete=models. CASCADE, default=1)
date = models. DateField(auto_now_add=True, auto_now=False, blank=True)
class Profile(models.Model) 。
def __str__(self)。
return str(self.user)
user = models. OneToOneField(User。null=True, on_delete=models. CASCADE)
date = models. DateField(auto_now_add=True。auto_now=False, blank=True)
projekt = models.ForeignKey(Projekt, on_delete=models. CASCADE, default=1)
class Projekt(models.Model) 。
def __str__(self):
return str(self.projekt)
projekt = models.TextField(max_length=150)
company_name = models.ForeignKey('Company', on_delete=models. CASCADE, default=1)
date = models. DateField(auto_now_add=True, auto_now=False, blank=True)
uj5u.com熱心網友回復:
下面是一個如何使用ORM的簡單例子,你可以根據你的情況進行調整:
假設我們有兩個模型,一個是Country,一個是City,像這樣:
class Country(models.Model)。
""國家模型""
name = models.CharField(...)
class City(models.Model)。
"" city model with a foreign key to a country """
country = models.ForeignKey('Country', ..., related_name='cities')
name = models.CharField(...)
population = models.PositiveIntegerField(...)
我們可以使用Django ORM,如下所示:
# filter for a country with a given name:
country = Country.objects.filter(name='Italy')
country.name # Italy(name='Italy)
# filter for a city with a given name:
city = City.objects.filter(name='Rome')
city.name # Rome(name='Rome')
# get the country the city belongs: 獲得城市所屬的國家。
country = city.country
country.name # 意大利
# get the queryset of all cities with a population greater than 100 million in a specific country:
country = Country.objects.filter(name='Italy')
large_cities = country.cities.filter(population__gt=1000000)
for city in large_cities:
print(city.name)
# 回圈將列印。羅馬,米蘭
另外,看起來你只是把一個Projeckt實體傳遞給你的模板。所以你真的不需要ORM,相反,你可以做一些類似的事情:
我也會簡單的說一下。
我還要簡單補充一下,你似乎顛倒了profile-projekt的關系--按照目前的情況,一個profile只能有一個procurement,但一個procurement可以有很多profile--不知道這是否是有意的。
views.py
def index(request)。
# get projekt by id:.
= projekt.objects.get(id=<the-id>)
模板 = loader.get_template('stressz/index.html'/span>)
背景關系 = {
'stomach': projekt,
}
return HttpResponse(template.render(text, request))
然后在你的模板中。 stressz/index.html
<!--在模板中使用背景關系資料-->。
<div>名稱。{{projekt.counter}}} </div>
<!--回圈查看專案中的所有組態檔 -->/span>
{% for profile in projekt.profile_set %}
<div> User First Name: {{ profile.user.first_name }}} </div> 用戶名:{{ profile.user.first_name }}.
<div> 用戶姓氏。{{profile.user.last_name}}用戶姓: </div> 用戶姓:{{ profile.user.last_name}}。
{% endfor %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/332037.html
標籤:
下一篇:使用聯合的子查詢的問題
