英文:
How to include all fields from foreign key in django model form
问题
我正在尝试从模型表单中访问与外键相关的字段。我已经阅读了文档,似乎我应该能够使用点表示法访问这些字段。我正在尝试的方式是否有意义?是否有更好的方法来做同样的事情。
HTML表单:
<form action="" method="post">
{% csrf_token %}
{{ form.room.room_available }}
{{ form.renter }}
<input type="submit" value="Submit">
</form>
Models.py:
class Room(models.Model):
roomNum = models.CharField(max_length=20)
room_is_premium = models.BooleanField(default=False)
room_available = models.BooleanField(default=False)
class BookedRoom(models.Model):
room = models.ForeignKey(Room, on_delete=models.CASCADE, null=True, blank=True)
renter = models.CharField(max_length=200)
start = models.BooleanField(default=True)
end = models.BooleanField(default=True)
Views.py:
def bookRoomNow(request, pk):
room = get_object_or_404(Room, pk=pk)
if request.method == "POST":
form = BookingForm(request.POST or None)
if form.is_valid():
form = form.save(commit=False)
form.room = room
form.save()
return redirect('room_details', pk=room.pk)
else:
form = BookingForm()
context = {"form": form}
return render(request, 'booker/booking_form.html', context)
请注意,上述内容已经被翻译为中文,只提供代码部分的翻译。
英文:
I am trying to access the fields related to a foreign key from a model form. I have read the docs and it seems I should be able to access these fields with dot notation. Does what I am attempting make sense? Is there a better way to do this same thing.
html form
{% csrf_token %}
{{ form.room.room_available }}
{{ form.renter }}
<input type="submit" value="Submit">
</form>
Models.py
class Room(models.Model):
roomNum = models.CharField(max_length=20)
room_is_premium = models.BooleanField(default=False)
room_available = models.BooleanField(default=False)
class BookedRoom(models.Model):
room = models.ForeignKey(Room, on_delete=models.CASCADE, null=True, blank=True)
renter = models.CharField(max_length=200)
start = models.BooleanField(default=True)
end = models.BooleanField(default=True)
Views.py
def bookRoomNow(request, pk):
room = get_object_or_404(Room, pk=pk)
if request.method == "POST":
form = BookingForm(request.POST or None)
if form.is_valid():
form = form.save(commit=False)
#this room.instance
form.room = room
form.save()
return redirect('room_details', pk=room.pk)
else:
form = BookingForm()
context= {"form":form}
return render(request, 'booker/booking_form.html', context)
答案1
得分: 1
The context variable form you are passing is largely concerned with the form itself - fields, errors etc.
You can pass the Room model instance itself as a separate context variable:
context = {"form": form, "room": room}
And then refer to its fields via dot notation
{{ room.room_available }}
However, if you want to display the form field itself (eg a HTML input with the current value set if there is one), and the field is included in your form definition, you can use
{{ form.renter }}
You can't follow directly from bookingform to room field to another field of the model room because the 'room field' can't be both a literal HTML form field in the booking form and also a model instance of a Room. The use of field in both cases is a little confusing perhaps (and also the bookedroom won't actually exist until after the form is submitted as your view is creating bookings, not editing them!)
英文:
The context variable form you are passing is largely concerned with the form itself - fields, errors etc.
You can pass the Room model instance itself as a separate context variable:
context= {"form":form, "room":room}
And then refer to its fields via dot notation
{{ room.room_available }}
However, if you want to display the form field itself (eg a HTML input with the current value set if there is one), and the field is included in your form definition, you can use
{{ form.renter }}
You can't follow directly from bookingform to room field to another field of the model room because the 'room field' can't be both a literal HTML form field in the booking form and also a model instance of a Room. The use of field in both cases is a little confusing perhaps (and also the bookedroom won't actually exist until after the form is submitted as your view is creating bookings, not editing them!)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论