[英]Django: how can i show the data of two or three models
hi working on a project where data is inherited from multiple models.您好,正在从事从多个模型继承数据的项目。 I don't know how to show the data of multiple models of a particular user.this is my first model user 1.
我不知道如何显示特定用户的多个模型的数据。这是我的第一个 model 用户 1。
class Loader_post(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="Loader", null=True)
pick_up_station = models.CharField(max_length=150)
destination_station = models.CharField(max_length=150)
sender_name = models.CharField(max_length=150)
phone_number = PhoneNumberField(null=False, blank=False, default='')
class price(models.Model):
my_post = models.ForeignKey(Loader_post, related_name='prices', on_delete=models.CASCADE,
null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, default='')
driver_price = models.CharField(max_length=150, null=True)
status = models.BooleanField(default=False)
class Booking(models.Model):
post = models.ForeignKey(price, related_name='b_post', on_delete=models.CASCADE,
default='', null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, default='')
approved_price = models.BooleanField(default=False)
Make sure you passed it in the views.py and imported it from.models.确保在views.py 中传递它并从.models 导入它。 Should look something like this:
应该看起来像这样:
from django.shortcuts import render
from .models import model1
def index(request):
model1 = model1.objects.all()
context = {'model1': model1}
render (request, '<template_name>', context)
And for the HTML part if you are sorting through a query it would look like this:对于 HTML 部分,如果您正在对查询进行排序,它将如下所示:
{% for object in model1 %}
{{ object }} # if you want a certain field it should be {{ object.<field_name> }} where <field_name> is the name of your field
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.