I wanted to use many to many and many to one with Django. Then I write this codes but when I try to add an Order from Admin Panel I got this error and I cannot find the solution. I hope someone can help me. In the below I share my models.py file I hope it will enough.
我想在Django上使用多对多和多对一。然后我写这些代码,但当我试图添加一个从管理面板的订单,我得到这个错误,我找不到解决方案。我希望有人能帮助我。在下面的文章中,我分享了我的mods.py文件,我希望它足够了。
from django.db import models
# Create your models here.
class Customer(models.Model):
name = models.CharField(max_length=200, null=True)
phone = models.IntegerField(null=True)
email = models.EmailField(null=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Tag(models.Model):
name=models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Product(models.Model):
STATUS = (
('Indoor','Indoor'),
('Out Door', 'Out Door')
)
name = models.CharField(max_length=200, null=True)
price = models.FloatField(null=True)
category = models.CharField(max_length=200, null=True,choices=STATUS)
description = models.CharField(max_length=200, null=True, blank=True)
date_created = models.DateTimeField(auto_now_add=True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.name
class Order(models.Model):
STATUS = (
('Pending','Pending'),
('Out for deliver', 'Out for delivery'),
('Delivered', 'Delivered')
)
customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
product = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
date_created = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=200, null=True, choices=STATUS)
def __str__(self):
return self.customer
I'm watching Dennis Ivy's Django Crash Course Tutorials and I did exactly what he did but he didn't get this error and I wonder what's the problem more than the solution. I read some tutorial about Many to one and Many to Many but I got nothing.
我正在看Dennis Ivy的Django速成课程教程,我做了他做的完全一样的事情,但他没有得到这个错误,我想知道除了解决方案,还有什么问题。我读了一些关于多对一和多对多的教程,但一无所获。
更多回答
优秀答案推荐
The __str__
method should return a string
__str__方法应返回一个字符串
Unfortunately, your Order __str__
method, returns a customer object.
不幸的是,您的Order__str__方法返回一个Customer对象。
def __str__(self):
return self.customer
To fix this, just name it return a string instead, eg,
要解决此问题,只需将其命名为返回一个字符串,例如,
def __str__(self):
return f"Order: {self.product.name} for {self.customer.name}"
# use return "Order: " + self.product.name + " for " + self.customer.name for older python versions
You should be able to also use
您还应该能够使用
def __str__(self):
return str(self.customer)
to access the customer objects __str__
function directly.
直接访问Customer对象__str__函数。
更多回答
It worked thank you. But I have one more question we only changed the last str not the others and they are working why didn't we change the others why are they working and this is not what is the difference ??
成功了,谢谢。但我还有一个问题,我们只改变了最后一串,而不是其他的,他们在工作,为什么我们不改变其他的,为什么他们在工作,而这不是,有什么区别?
Look at what the others are returning. The Customer __str__
returns self.name. Customer.name is a character field, and so a string. Order __str__
returned self.customer, the Customer object theat the forign key referred to - an object not a string. I think you might also have been able to use return str(self.customer)
to ask for that customer's __str__
function directly.
看看其他人都退回了什么。Customer__str__返回self.name。名称是一个字符字段,因此是一个字符串。ORDER__STR__返回self.Customer,Customer对象位于引用的前缀-对象而不是字符串。我认为您还可以使用返回字符串(self.Customer)直接请求客户的__str__函数。
我是一名优秀的程序员,十分优秀!