Django: How to set a hidden field on a generic create view?

前端 未结 3 1336
情深已故
情深已故 2021-01-06 09:31

I\'m running Django 1.6.x

To extend my user I\'ve added another model storing the data:

class UserProfile (models.Model):
    user = models.ForeignK         


        
3条回答
  •  礼貌的吻别
    2021-01-06 09:56

    class UserProfileCreateView(CreateView):
        def form_valid(self, form):
             self.object = form.save(commit=False)
             self.object.user = self.request.user
             self.object.save()
             return super(ModelFormMixin, self).form_valid(form)
    

提交回复
热议问题