gpt4 book ai didi

Django 'QuerySet' 对象没有属性 'split' 使用 modelform

转载 作者:行者123 更新时间:2023-12-04 18:13:41 27 4
gpt4 key购买 nike

我无法让我的 View 更新多对多字段。它在表单提交后返回。

Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/footbook/Ubuntu One/webapps/fb/poc/../poc/activity/views.py" in activity_save_page
44. group_names = form.cleaned_data['groups'].split()

Exception Type: AttributeError at /activity_save/
Exception Value: 'QuerySet' object has no attribute 'split'

这是文件。
模型.py
class Group (models.Model):
group_nm = models.CharField(max_length=64)
group_desc = models.CharField(max_length=250)
created = models.DateTimeField(auto_now_add=True)
active_yn = models.BooleanField(default=True)
def __unicode__(self):
return self.group_nm



class Activity(models.Model):
activity_nm = models.CharField(max_length=60)
activity_desc = models.CharField(max_length=250)
startdt = models.DateField()
enddt = models.DateField()
crdt = models.DateTimeField(auto_now_add=True,editable=False)
groups = models.ManyToManyField(Group)
upddt = models.DateTimeField(editable=False)
def save(self, *args, **kwargs):
if not self.id:
self.crdt = datetime.date.today()
self.upddt = datetime.datetime.today()
super(Activity, self).save(*args, **kwargs)
def __unicode__(self):
return self.name

表格.py
def make_custom_datefield(f):
formfield = f.formfield()
if isinstance(f, models.DateField):
formfield.widget.format = '%m/%d/%Y'
formfield.widget.attrs.update({'class':'datePicker', 'readonly':'true'})
return formfield


class ActivitySaveForm(forms.ModelForm):
formfield_callback = make_custom_datefield
def __init__(self, *args, **kwargs):
super(ActivitySaveForm, self).__init__(*args, **kwargs)
self.fields['activity_nm'].label = "Activity Name"
self.fields['activity_desc'].label = "Describe It"
self.fields['startdt'].label = "Start Date"
self.fields['enddt'].label = "End Date"
self.fields['groups'].label ="Group"
class Meta:
model = Activity

View .py
def activity_save_page(request):
if request.method == 'POST':
form = ActivitySaveForm(request.POST)
if form.is_valid():
act, created = Activity.objects.get_or_create(
activity_nm = form.cleaned_data['activity_nm']
)
act.activity_desc = form.cleaned_data['activity_desc']
if not created:
act.group_set.clear()
group_names = form.cleaned_data['groups'].split()
for group_name in group_names:
group, dummy = Group.objects.get_or_create(group_nm=group_name)
act.group_set.add(group)
act.save()
return HttpResponseRedirect('/activity/')
else:
form = ActivitySaveForm()
variables = RequestContext(request, {
'form': form
})
return render_to_response('activity_save.html', variables)

我认为如果我不使用模型表单它会起作用,但我需要它来实现 this datepicker.由于它是一个多对多字段,我想在将它们输入数据库时​​将它们拆分,但我的查询集失败了。我试过用很多不同的方式改变它,但我被卡住了。我见过很多类似的问题,但它们要么有外键,要么没有模型形式。

谢谢。

编辑:
事件保存.html
{% extends "base.html" %}
{% block title %}Save Activity{% endblock %}
{% block head %}Save Activty{% endblock %}
<input class="datePicker" readonly="true" type="text" id="id_startdt" />
<input class="datePicker" readonly="true" type="text" id="id_enddt" />
{% block content %}


<form action="{% url activity.views.activity_save_page act_id%}" method="post">{% csrf_token %}

{{ form.as_p }}

<input type="submit" value="save it" />
</form>
{% endblock %}

最佳答案

正如错误描述的那样:QuerySet没有 split方法。您不能调用 my_qs.split() .
form.cleaned_data['groups']返回清理后的数据;它已经为您处理了字符串到 python 对象的形式转换,在 ManyToManyField 的情况下最终由 QuerySet 表示在 python 。

日期字段通过表单清理以相同的方式返回日期对象,IntegerField 为整数,CharFields 为字符串等。

如果您想要 group_names 的列表,您需要显式地遍历 QuerySet 中的对象并拉他们的group_nm属性。

            group_names = [x.group_nm for x in form.cleaned_data['groups']]

关于Django 'QuerySet' 对象没有属性 'split' 使用 modelform,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12127662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com