Python 多对多关系中的Django管理错误
比如说Python 多对多关系中的Django管理错误,python,django,many-to-many,django-orm,Python,Django,Many To Many,Django Orm,比如说 class One(models.Model): text=models.CharField(max_length=100) class Two(models.Model): test = models.Integer() many = models.ManyToManyField(One, blank=True) 当我尝试在“管理”面板中保存对象时,出现如下错误: “两个”实例需要有一个主键值,才能使用多对多关系 我使用django 1.3。我试着
class One(models.Model):
text=models.CharField(max_length=100)
class Two(models.Model):
test = models.Integer()
many = models.ManyToManyField(One, blank=True)
当我尝试在“管理”面板中保存对象时,出现如下错误:
“两个”实例需要有一个主键值,才能使用多对多关系
我使用django 1.3。我试着在两个类中添加AutoField,但它也不起作用
这是我的密码
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, redirect
from django.template import RequestContext
from django.core.urlresolvers import reverse
from project.foo.forms import FooForm
from project.foo.models import Foo
from project.fooTwo.views import fooTwoView
def foo(request, template_name="foo_form.html"):
if request.method == 'POST':
form = FooForm(data=request.POST)
if form.is_valid():
foo = Foo()
foo.name = request.POST.get("name")
foo.count_people = request.POST.get("count_people")
foo.date_time = request.POST.get("date_time")
foo.save()
return fooTwoView(request)
else:
form = FooForm()
return render_to_response(template_name, RequestContext(request, {
"form": form,
}))
另外,我发现我的失败了。它在模型中。我在保存方法中使用了多对多。我在使用前添加了检查功能,但没有帮助
class Foo(models.Model):
name = models.CharField(max_length=100, null=False, blank=False)
count_people = models.PositiveSmallIntegerField()
menu = models.ManyToManyField(Product, blank=True, null=True)
count_people = models.Integer()
full_cost = models.IntegerField(blank=True)
def save(self, *args, **kwargs):
if(hasattr(self,'menu')):
self.full_cost = self.calculate_full_cost()
super(Foo, self).save(*args, **kwargs)
def calculate_full_cost(self):
cost_from_products = sum([product.price for product in self.menu.all()])
percent = cost_from_products * 0.1
return cost_from_products + percent
我尝试在保存方法中使用hack,例如
if(hasattr(self,Two)):
self.full_cost = self.calculate_full_cost()
这是帮助我,但我不认为这是django的方式。有趣的是,没有这个检查管理面板显示错误,而是创建对象。现在,如果我从两个项目中选择项目并保存,我的对象没有全部成本,但当我查看我的对象时,管理面板记住我的选择并显示我的两个项目,我选择的内容。。。我不知道为什么
如何保存此文件?为什么不使用OneToOneField而不是多对多您的代码有很多问题。最明显的是 1/在您看来,使用表单进行用户输入验证/净化/转换,然后忽略已净化/转换的数据,直接从请求中获取未净化的输入。使用form.cleaned_data而不是request.POST来获取数据,或者更好地使用ModelForm来为您创建一个完全填充的Foo实例 2/Python方法中没有隐式this或self或任何指针,您必须显式地使用self来获取实例属性。以下是您的模型的保存方法的实际功能:
def save(self, *args, **kwargs):
# test the truth value of the builtin "id" function
if(id):
# create a local variable "full_cost"
full_cost = self.calculate_full_cost()
# call on super with a wrong base class
super(Banquet, self).save(*args, **kwargs)
# and exit, discarding the value of "full_cost"
现在,关于你的问题:Foo.save显然不是基于m2m相关对象计算某些内容的正确位置。或者编写一个运行计算和更新Foo的独特方法,保存它,并在保存m2m后调用它提示:ModelForm将为您保存m2m相关对象,或者只使用m2m_changed信号
话虽如此,我强烈建议您花几个小时学习Python和Django-这将为您节省大量时间。当我们使用订单和产品时,如何使用OneTONE?一个订单有很多产品,一个产品可以有很多订单。我不知道你的代码还有更多。下次导入整个模型代码:非常感谢。后来我把代码改成了正确的形式,但我忘了更新这篇文章。感谢您对form.u data和form.save的建议,我将使用这个我只是复制\粘贴相同的代码并使用他*m2m呢。你们告诉写方法,更新,保存等将做什么。你们能展示一个例子或链接,我可以阅读有关这个?m2m_改变信号怎么样?谢谢。也许我会在我的解决方案中使用它。文档中有很多代码示例和解释,您应该从阅读开始。