Django ManyToManyField在shell上工作,但不在服务器上工作
这是我见过的罕见的行为。我在一张桌子上有两个很多关系:Django ManyToManyField在shell上工作,但不在服务器上工作,django,manytomanyfield,Django,Manytomanyfield,这是我见过的罕见的行为。我在一张桌子上有两个很多关系: # models.py ... class Customer(models.Model): name = models.CharField('Name', max_length=128) ... class Report(models.Model): ... companies = models.ManyToManyField(Company) customers = models.ManyTo
# models.py
...
class Customer(models.Model):
name = models.CharField('Name', max_length=128)
...
class Report(models.Model):
...
companies = models.ManyToManyField(Company)
customers = models.ManyToManyField(Customer)
...
该表单是普通表单,没有自定义的save()
当我通过webapp创建报表对象时,我可以在shell上成功查询它们:
In [3]: report.customers.all()
Out[3]: <QuerySet [<Customer: xxxxxxx>]>
这里是模板
# report_list.html
{% for object in object_list %}
<td>
{% for customer in object.customers.all %}
<span class="label label-success">{{ customer.name }}</span>
{% endfor %} <-- This doen't work -->
</td>
<td>
{% for company in object.companies.all %}
<span class="label label-success">{{ company.name }}</span>
{% endfor %} <-- This works :-O -->
</td>
{% endfor %}
#report_list.html
{对象列表%中的对象的百分比}
{object.customers.all%中的客户为%s}
{{customer.name}
{%endfor%}事实上,它与另一个应用程序中的另一个表存在冲突。在我的第一个应用程序中:
# app1 models.py
...
class Customer(models.Model):
name = models.CharField('Name', max_length=128)
...
class Report(models.Model):
...
companies = models.ManyToManyField(Company)
customers = models.ManyToManyField(Customer)
...
# app2 models.py
...
from app1.models import Customer
class Report(models.Model):
...
customer = models.ForeignKey(Customer)
...
在另一个应用程序中,我有:
# app1 models.py
...
class Customer(models.Model):
name = models.CharField('Name', max_length=128)
...
class Report(models.Model):
...
companies = models.ManyToManyField(Company)
customers = models.ManyToManyField(Customer)
...
# app2 models.py
...
from app1.models import Customer
class Report(models.Model):
...
customer = models.ForeignKey(Customer)
...
我只需要在app2外键中添加一个特定的相关名称
。始终建议添加此唯一的相关\u名称
,以避免此类问题:-S您可以共享视图代码吗?我刚刚在问题中添加了ListView代码。如您所见,是一个常规列表视图请向我们展示客户
模型-特别是名称
字段是如何定义的。@Solarismoke我刚刚编辑了问题,以便您可以查看客户
模型。