Не пинайте сразу, я начинающий. Вопрос такой, есть формы надо их значения сохранить в базу, при сохранении ругается:
Cannot assign “u'Моя строка'”: “Projects.owner” must be a “User” instance.
Вот мой код:
(forms.py)
from django import forms from django.forms.extras.widgets import SelectDateWidget class CreateProjectWizzard(forms.Form): owner_form = forms.CharField(label='Владелец', max_length=138, required=False) projectName_form = forms.CharField(label='Краткое наименование проекта', max_length=138, required=False) startDate_form = forms.DateField(label='Дата начала проекта', widget=SelectDateWidget(years=range(2010, 2030)), required=False) endDate_form = forms.DateField(label='Дата окончания проекта', widget=SelectDateWidget(years=range(2010, 2030)), required=False)
(models.py)
class Projects(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u'Владелец') projectName = models.CharField(u'Наименование проекта', max_length=100) startDate = models.DateField(u'Дата начла проекта') endDate = models.DateField(u'Дата окончания проекта', blank=True, null=True) def __unicode__(self): return self.projectName class Meta: ordering = ['projectName']
(views.py)
@login_required def createProject(request): if request.method == 'POST': forms = CreateProjectWizzard(request.POST) if forms.is_valid(): f_owner = forms.cleaned_data['owner_form'] f_projectName = forms.cleaned_data['projectName_form'] f_startDate = forms.cleaned_data['startDate_form'] f_endDate = forms.cleaned_data['endDate_form'] p = Projects(owner=f_owner, projectName=f_projectName, startDate=f_startDate, endDate=f_endDate) p.save() return render_to_response('projectManager/pmIndex.html', { 'username': request.user.get_full_name, }, context_instance=RequestContext(request)) return render_to_response('projectManager/createProject.html', { 'forms': CreateProjectWizzard, 'username': request.user.get_full_name, }, context_instance=RequestContext(request))