Django:无法在CBV中保存相关模型

时间:2012-09-12 05:29:25

标签: django django-class-based-views

我有两个与this case非常相似的模型:

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())

1 个答案:

答案 0 :(得分:3)

save方法中event.locationNone。您需要创建location 实例然后保存它。

更新:用于保存现有对象:

我不确定在阅读Generic views - Models后,您UpdateView的实施方式是否可行

我建议将视图更改为:

class EventEdit(UpdateView):
    model = Event

    def form_valid(self, form):
        #instance trying to update
        event = form.instance 
        location = event.location
        if location == None:
            location = Location()
        location.city = self.cleaned_data['city']
        location.address = self.cleaned_data['address']
        location.save()
        event.location = location
        #event.save() instead of this do
        super(EventEdit, self).form_valid(form)
        return HttpResponseRedirect(self.get_success_url())

旧解决方案:

我会将保存方法更改为

def save(self, commit=True):
    event = super(EventForm, self).save(commit=False)
    location = Location()
    location.city = self.cleaned_data['city']
    location.address = self.cleaned_data['address']
    location.save()
    event.location = location
    event.save()
    return event