django的超级新手,从事我的第一个测试项目。 我有一个称为客户的应用程序。在里面,我有几个模型。我现在要开始工作时,主要的2个是Customer_Info和Company_Info。 在模板方面,我想从两个模板的for循环中提取信息。公司可以有多个客户(联系人)。因此,在客户信息表中,我存储了他们可能所属的公司uuid。 一些客户不会拥有该ID,因为他们是个人并且没有公司协会。
这是2种型号。
class Customer_Info(models.Model): #Name of the table
id = models.AutoField(primary_key=True)
cust_uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
first_name = models.CharField(max_length=32, blank=False)
middle_name = models.CharField(max_length=32, blank=True)
last_name = models.CharField(max_length=32, blank=False)
phone = models.CharField(max_length=10, blank=False)
email = models.CharField(max_length=64, blank=False)
address_1 = models.CharField(max_length=64, blank=True, null=True)
address_2 = models.CharField(max_length=64, blank=True, null=True)
city = models.CharField(max_length=16, blank=True, null=True)
state = models.CharField(max_length=2, blank=True, null=True)
zipcode = models.IntegerField(blank=True, null=True)
country = models.CharField(max_length=16, blank=True, null=True)
company_uuid = models.ForeignKey('company_info', to_field='company_uuid', db_column='company_uuid', on_delete=models.PROTECT, blank=True, null=True) # miht need a nullable defaul https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, blank=False) # miht need a something else https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey
date_added = models.DateTimeField(auto_now=True)
def __str__(self):
return 'Customer ID: {0} | First Name: {1} | Last Name: {2} | Phone: {3} | Email: {4}'.format(self.cust_uuid, self.first_name, self.last_name, self.phone, self.email)
class Company_Info(models.Model):
id = models.AutoField(primary_key=True)
company_uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
company_name = models.CharField(max_length=64, blank=False)
website = models.CharField(max_length=200, blank=True, null=True)
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, blank=False) # miht need a something else https://docs.djangoproject.com/en/2.1/ref/models/fields/#foreignkey
date_added = models.DateTimeField(auto_now=True)
def __str__(self):
return 'Company ID: {0} | Company Name: {1}'.format(self.company_uuid, self.company_name)
这就是我要查看的内容。
def customers(request):
company_customer_info = {
'companies': Company_Info.objects.all(),
'customers': Customer_Info.objects.all(),
}
return render(request, 'customers/customers.html', company_customer_info)
这是模板中的内容。
这是我正在工作的内容,但是我不知道如何提取联系人姓名...
{% for company in companies %}
<tr>
<td>{{ company.id }}</td>
<td>{{ company.company_name }}</td>
<td>contact name here</td>
<td>{{ company.website }}</td>
<td>{{ company.date_added|date:"N d, Y" }}</td>
</tr>
{% endfor %}
在同一页面上,我也有这个,并且可以正常工作。
{% for customer in customers %}
<tr>
<td>{{ customer.id }}</td>
<td>{{ customer.first_name }}</td>
<td>{{ customer.last_name }}</td>
<td>{{ customer.company_uuid.company_name}}</td>
<td>{{ customer.phone }}</td>
<td>{{ customer.email }}</td>
<td>{{ customer.date_added|date:"N d, Y" }}</td>
</tr>
{% endfor %}
我们非常感谢您的帮助,我一直试图将查询文档上的django文档包起来,但是我走得还很远...