我有一个名为Project
的模型,其中包含三个ManyToManyField
s member_student
,supervisor
和tag
)。其中一个(tag
)被排除在表单中,必须手动保存。因此,在视图中,我使用save(commit=False)
因为我必须更改表单的某些字段。更改字段后,表单将保存,我会逐个添加标签。然后,当我致电save_m2m
保存ManyToManyField
时,我会收到视图中save_m2m
行给出的错误:
invalid literal for int() with base 10: 'a'
这是我的模特。
class Tag(models.Model):
name = models.CharField(unique=True, max_length=60)
slug = models.SlugField(max_length=60, unique=True)
def save(self, *args, **kwargs):
if not self.id:
self.slug = slugify(self.name)
super(Tag, self).save(*args, **kwargs)
class Project(models.Model):
'''Main Project uploading'''
title = models.CharField(max_length=300)
description = models.TextField(null=True)
#year = models.ForeignKey(Year)
tag = models.ManyToManyField(Tag)
owner_student = models.ForeignKey(Student, related_name='member_student')
member_student = models.ManyToManyField(Student, blank=True, null=True)
supervisor = models.ManyToManyField(Supervisor, blank=True, null=True)
subject = models.ForeignKey(Subject)
main_document = models.FileField(upload_to='main_documents/')
supporting_document = models.FileField(upload_to='supp_documents/', blank=True, null=True)
source_code = models.FileField(upload_to='source_code/', blank=True, null=True)
screenshot = models.ImageField(upload_to='screenshots/', blank=True, null=True)
这是forms.py
:
class ProjectForm(forms.ModelForm):
class Meta:
model = Project
exclude = ['owner_student', 'slug', 'tag']
tag = forms.CharField(max_length=100)
这是视图。
def add_project(request):
parameters = {}
if request.method =="POST":
upload_form = ProjectForm(request.POST, request.FILES)
if upload_form.is_valid():
new_form = upload_form.save(commit=False)
mystud = Student.objects.get(user=request.user)
new_form.owner_student = mystud
new_form.save()
tags = upload_form.cleaned_data['tag']
tags = tags.split(',')
for eachtag in tags:
tag, created = Tag.objects.get_or_create(name=eachtag.strip())
tag.save()
new_form.tag.add(tag)
upload_form.save_m2m()
return HttpResponseRedirect(reverse(project_page, args=(new_form.slug,)))
else:
parameters["upload_form"] = upload_form
return render_to_response('upload.html', parameters)
else:
upload_form = ProjectForm()
parameters["upload_form"] = upload_form
parameters["page_title"] = "Upload your Project"
return render_to_response('upload.html', parameters)
所以,我的问题是如何在不出错的情况下保存标签以及另外两个ManyToManyField
?我猜save_m2m
函数由于get_or_create
返回的元组而给出错误。
答案 0 :(得分:2)
请勿使用“tag”作为表单上字段的名称。这将导致save_m2m
认为需要使用字段中的值来设置对象上的相关“标记”字段。
在内部,save_m2m
遍历模型中的每个多对多字段。它在表单的cleaned_data
字典中检查该名称下是否存在数据,如果存在,则模型字段对象更新使用字段的save_form_data
方法记录内容。它信任表单字段以返回正确类型的Python对象。在这种情况下,您的charfield返回一个字符串(如预期的那样),但将字符串分配给多对多字段是不正确的。