我想從我的資料庫中訪問 table1 中的資料,并使用 django 基于其他表進行查詢。例如:從表 1(用戶、日期、小時:分鐘、城市、標簽)和表 2(用戶、日期:小時:分鐘、緯度、經度)訪問資料,如果兩個表中的小時:分鐘相同,我需要使用一些值(北、南、東、西)更新欄位標簽。我嘗試在用戶、日期上使用 objects.filter 訪問 table1 中的資料,但我認為這不是一個好方法,因為它沒有給我行中的值。我從https://docs.djangoproject.com/en/4.0/topics/db/queries/#copying-model-instances閱讀了檔案但我無法正確地做到這一點并完成它。對于第二部分,我想用 if 做點什么,但仍然不確定。一開始我需要訪問table1中的資料。
編輯:當我上傳 excel 檔案時,我想在檔案 uploadCoord_File_view 中進行此更改。 模型.py
class Localization(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
date = models.CharField(max_length=10, blank=False, null= False)
time = models.CharField(max_length=5, blank=False, null= False)
city = models.CharField(max_length=10, blank=False,, null = True)
tag = models.CharField(max_length=10, default=None, null = True)
def __str__(self):
return f"{self.user}"
class Coordinates(models.Model):
user = models.ForeignKey(UserAccount, related_name='hr_user', on_delete=models.CASCADE)
date = models.CharField(max_length=10, blank=False, null= False)
time = models.CharField(max_length=5, blank=False, null= False)
latitude = models.CharField(max_length=10, blank=False, null= False)
longitudinal = models.CharField(max_length=10, blank=False, null= False)
def __str__(self):
return f"{self.user}"
視圖.py
@login_required(login_url='login')
def uploadLocalization_file_view(request):
form = CsvFileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
form = CsvFileForm()
obj = CsvFileModel.objects.get(activated=False)
with open(obj.file_name.path, 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i==0:
pass
else:
date = row[0]
user = User.objects.get(id = user_id)
Localization.objects.create(
date=date,
time = row[1],
city = row[2],
)
t= convert_time2sec(row[1])
obj.activated=True
obj.save()
return redirect('../uploadCoord_file_view')
return render(request, 'uploadFile.html', {
'importFileForm': form
})
@login_required(login_url='login')
def uploadCoord_file_view(request):
form = CsvFileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
form = CsvFileForm()
obj = CsvFileModel.objects.get(activated=False)
with open(obj.file_name.path, 'r') as f:
reader = csv.reader(f)
for i, row in enumerate(reader):
if i==0:
pass
else:
date = row[0]
user = User.objects.get(id = user_id)
Coordinates.objects.create(
date=date,
time = row[1],
latitude = row[2],
longitudinal = row[3],
)
t = convert_time2sec(row[1])
###### Here I need to access the table Localization and create the query
obj.activated=True
obj.save()
return redirect('../results')
return render(request, 'uploadCoordFile.html', {
'importCoordFileForm': form
})
uj5u.com熱心網友回復:
我無法弄清楚user = User.objects.get(id = user_id)從哪里來user_id。它是來自reader還是你可以使用user = request.user?
如果我的假設是正確的,那么我認為您可以在回圈之前先獲取 User、UserAcount 和 Localization 表for:
@login_required(login_url='login')
def uploadCoord_file_view(request):
# Get the user
user = request.user
# Access the Localization table of the specific user
localization = Localization.objects.get(user=request.user)
# Access the UserAccount (you did not provide that model, so I am guessing
user_account = UserAccount.objects.get(user=request.user)
...
然后你可以像這樣進行匹配和更新:
###### Here I need to access the table Localization and create the query
# Filter all Coordinates that have the same date and user as the Localization table
coordinates = Coordinates.objects.filter(date=localization.date, user=user_account)
# Update the values of all the matches
coordinates.update(latitude='North', longitude='West')
最好將所有get查詢放在try/exceptget陳述句中,否則如果回傳不匹配,您將收到錯誤:
try:
# Access the Localization table of the specific user
localization = Localization.objects.get(user=request.user)
except Localization.DoesNotExist
# Code to handle what should happen if no match exists
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/470349.html
