我有两个与这种情况非常相似的模型:
class Location(models.Model):
city = models.CharField(max_length=20)
address = models.CharField(max_length=30)
class Event(models.Model):
location = models.ForeignKey(Location)
date = models.DateField()
user = models.ForeignKey(User)
我试图以形式保存这些对象:
class EventForm(forms.ModelForm):
city = forms.CharField(label=_('City'), max_length=30)
address = forms.CharField(label=_('Street'), max_length=30, required=False)
class Meta:
model = Event
def __init__(self, *args, **kwargs)
super(EventForm, self).__init__(*args, **kwargs)
try:
self.fields['city'].initial = self.instance.location.city
self.fields['address'].initial = self.instance.location.street
except AttributeError:
pass
def save(self, commit=True):
event = super(EventForm, self).save(commit=False)
location = event.location
location.city = self.cleaned_data['city']
location.address = self.cleaned_data['address']
location.save()
return event
这会引发错误'NoneType' object has no attribute 'city'
我还尝试在 CBV 中保存位置:
class EventEdit(UpdateView):
model = Event
def form_valid(self, form):
event = form.save(commit=False)
location = event.location
location.city = self.cleaned_data['city']
location.address = self.cleaned_data['address']
location.save()
event.save()
return HttpResponseRedirect(self.get_success_url())
同样的错误'NoneType' object has no attribute 'city'
在基于类的视图中保存相关对象的正确方法是什么?
更新
我必须补充一点,我正在询问更新分配给事件的现有位置。EventCreate(CreateView)
完全按照Rohan的建议添加新的活动地点。
class EventCreate(CreateView):
def form_valid(self, form):
self.object = form.save(commit=False)
location = Location()
location.address = self.request.POST['address']
location.city = self.request.POST['city']
location.save()
self.object.location = location
self.object.save()
return HttpResponseRedirect(self.get_success_url())