2

我有两个与这种情况非常相似的模型:

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

1 回答 1

3

在你的save方法event.location中将是None. 您需要创建location 实例,然后保存它。

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

我不确定您的实现是阅读通用视图 - 模型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
于 2012-09-12T05:39:50.457 回答