Форум сайта python.su
Всем привет, направьте на верный путь, видимо уже запутался.
У меня есть модель
class PortalUser(AbstractBaseUser, PermissionsMixin): pass #some fields
class PortalUserProducts(models.Model): portal_user_product_owner = models.ForeignKey(PortalUser) #other_fields....
class PortalUserProductsForm(forms.ModelForm): class Meta: model = PortalUserProducts fields = '__all__' exclude = [ 'portal_user_product_owner', ]
class AddPortalUserProducts(CreateView): form_class = PortalUserProductsForm template_name = 'addproduct.html' success_url = '/profile/' '''# Disabled method def get_form_kwargs(self): kwargs = {'initial': self.get_initial()} if self.request.method in ('POST', 'PUT'): kwargs.update({ 'portal_user_product_owner': self.request.POST, #'files': self.request.FILES, }) return kwargs''' def get_initial(self): return {'portal_user_product_owner': self.request.user} @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(AddPortalUserProducts, self).dispatch(*args, **kwargs) def get_object(self, queryset=None): return self.request.user
Офлайн
Разобрался
def form_valid(self, form): form.instance.portal_user_product_owner = self.request.user return super(AddPortalUserProducts, self).form_valid(form)
Офлайн