0
我有一個名爲Listing
的模型,它基本上是由我的用戶發佈的職位列表。其中一個字段是一個名爲Address的外鍵(因爲用戶可以有多個地址)。這裏是我的models.py:Django外鍵下拉篩選器
class JobListing(models.Model):
title = models.CharField(max_length=250)
description = models.TextField()
customer = models.ForeignKey('CustomerAuth.CustomerProfile')
address = models.ForeignKey('CustomerAuth.Address')
這裏是我的forms.py:
class JobListingForm(forms.ModelForm):
class Meta:
model = JobListing
fields = ['title', 'description', 'address', 'customer']
最後我views.py的相關部分:
def create_listing(request):
form = JobListingForm(request.POST or None)
if form.is_valid():
listing = form.save(commit=False)
listing.save()
current_user = CustomerProfile.objects.get(user=request.user)
listing.customer = current_user
listing.save()
return HttpResponseRedirect('/listings/id/%s' % listing.id)
context_dict = {'form': form}
return render(request, 'listing/create-listing.html', context_dict)
因爲它目前爲當用戶從下拉列表中選擇一個地址時,它會顯示每個人的地址。無論如何,我可以過濾它,以便只顯示該用戶輸入的地址。
對不起,我有點新的Django,我補充這我的看法? – ng150716
是的,你想把結果發送到html文件 – Exprator
不要對不起兄弟。我們犯錯誤,然後只學習。即使我不知道很多東西。學習當我也面臨問題。 – Exprator