I am having trouble saving a custom field in a ModelForm. The field in question is a ModelChoiceField.
I have added a save() method as shown in this question. However, when I use it I get an error:
ImproperlyConfigured
No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model.
When I remove my custom save() method it works ok but doesn't save the custom field. What am I missing?
class NewStoryForm(forms.ModelForm):
class Meta:
model = Story
fields = ['title', 'story_text']
#custom field
about = forms.ModelChoiceField(queryset=None)
#initialise custom field
def __init__(self, user, *args, **kwargs):
super(NewStoryForm, self).__init__(*args, **kwargs)
self.fields['about'] = forms.ModelChoiceField(queryset=Experience.objects.filter(user=user))
#save custom field
def save(self, commit=True):
self.instance.about = self.cleaned_data['about']
super(NewStoryForm, self).save(commit=commit)
class NewStoryView(CreateView):
form_class = NewStoryForm
template_name = 'story/story_form.html'
#Send user to NewStoryForm to initialise custom field
def get_form_kwargs(self):
kwargs = super(NewStoryView, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
#save author as current user
def form_valid(self, form):
form.instance.author = self.request.user
return super(NewStoryView, self).form_valid(form)
You should return the saved object from the save() method:
return super(NewStoryForm, self).save(commit=commit)
Related
So I have this view:
class ProfileView(generic.UpdateView):
model = User
fields = [....]
template_name_suffix = '_update_form'
success_url = reverse_lazy('home')
def post(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.is_active = False
return super().post(request, *args, **kwargs)
when the user saves his data on update, I want some fields to be completed automatically, such as is_active = False.
I used the approach above but my inserted fields aren't changed.
Why and how can I get the desired result?
Thanks.
There will be two objects here: the one wrapped in the form, and the one you use in the .post method, and you save the one in the form.
You can override the .form_valid(…) method [Django-doc]:
class ProfileView(generic.UpdateView):
model = User
fields = # …
template_name_suffix = '_update_form'
success_url = reverse_lazy('home')
def form_valid(self, form):
form.instance.is_active = False
return super().form_valid(form)
I created a FormView and it works fine if the user executed the process the first time. However when it is executed the second time I get an error that the record already exist. This is expected as the user in the model is unique. How can I overcome this problem so that the current record is overwritten by the form.save if the record already exist.
models.py
class ttemp_selection(models.Model):
select_account = models.ForeignKey(tledger_account, on_delete=models.CASCADE)
date_from = models.DateField(default=datetime.today)
date_to = models.DateField(default=datetime.today)
user = models.ForeignKey(custom_user, on_delete=models.CASCADE, unique=True)
def __str__(self):
return self.select_account
forms.py
class Meta:
model = ttemp_selection
fields = ['select_account', 'date_from', 'date_to', 'user']
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request')
super(SelectAccountForm, self).__init__(*args, **kwargs)
user = self.request.user
current_company = user.current_company
self.fields['select_account'].queryset = tledger_account.objects.filter(
company=current_company, gl_category='Cash and Bank')
view.py
class sasView(FormView):
template_name = 'cashflow/select_account.html'
form_class = SelectAccountForm
success_url = 'home'
def form_valid(self, form):
form.save()
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(sasView, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
I can determine the record by using ttemp_selection.objects.get(user=request.user)
I know I can make use of the UpdateView class but that will create a problem when the record does not exist. It will also add an extra step that is unnecessary.
Assistance will be appreciated.
You can work with a CreateView, and slightly alter the behavior to specify a self.object if that exists:
from django.contrib.auth.mixins import LoginRequiredMixin
class sasView(LoginRequiredMixin, CreateView):
template_name = 'cashflow/select_account.html'
form_class = SelectAccountForm
success_url = 'home'
def get_form(self, *args, **kwargs):
self.object = ttemp_selection.objects.filter(
user=self.request.user
).first()
return super().get_form(*args, **kwargs)
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
def get_form_kwargs(self):
kwargs = super(sasView, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
It however makes no sense to include the user as field, since - if I understand it correctly - you use the logged in user. By including it, you make it possible that a person forges a POST request, and thus changes the account of a different user. You should omit this filed:
class SelectAccountForm(forms.ModelForm):
class Meta:
model = ttemp_selection
# no user ↓
fields = ['select_account', 'date_from', 'date_to']
# …
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin mixin [Django-doc].
I have Form with a ModelChoiceField, which is being used as the form_class in a FormView.
The choice field has to be populated with information bound to the request object.
Let's summarize:
class MyFormView(FormView):
# I need to pass `request.user` and a value
# derived from `request.GET['pk']` to the form
form_class = MyForm
class MyForm(Form):
choices = ModelChoiceField(queryset=MyChoice.objects.none())
def __init__(self, user, number, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['choices'] = MyChoice.objects(number=number, owner=user)
What would I need to do to pass that data to the form when its instance is created?
I tried overriding get_form but I am unsure this is the proper way of doing this:
def get_form(self, form_class):
user = self.request.user
number = SomeModel.objects.get(self.GET['pk']).number
return form_class(user, number, **self.get_form_kwargs())
Overriding get_form would work, but a better approach would be to override get_form_kwargs, so that you don't have to duplicate code from the get_form method.
class MyFormView(FormView):
form_class = MyForm
def get_form_kwargs(self):
kwargs = super(MyFormView, self).get_form_kwargs()
kwargs['user'] = self.request.user
kwargs['number'] = SomeModel.objects.get(self.GET['pk']).number
return kwargs
I have a ModelForm with a custom save method to populate a model field with a kwarg from the url params (that was passed to the form):
from app.models import MyModel
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.fk_customer = kwargs.pop('customer')
super(MyModelForm, self).__init__(*args, **kwargs)
class Meta:
model = MyModel
fields = '__all__'
def clean(self):
cleaned_data = super(MyModelForm, self).clean()
cleaned_data['fk_customer'] = self.fk_customer
return cleaned_data
When I inspect cleaned_data in my view, fk_customer is present and valid. However is_valid() is false, and the ModelForm won't save(). If I override a few things and force save, the field fk_customer is saved as None.
What's going on and how can I alter cleaned_data and still validate?
If you are not displaying the customer field from your form, then you should exclude it from your form class instead of using __all__.
Then, I would try to set the customer in the form's save method instead of the clean method. The following is untested:
class MyModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.customer = kwargs.pop('customer')
super(MyModelForm, self).__init__(*args, **kwargs)
class Meta:
model = MyModel
exclude = ('customer',)
def save(self, commit=True)
instance = super(MyModelForm, self).save(commit=False)
instance.customer = self.customer
if commit:
instance.save()
self.save_m2m()
return instance
In a class-base UpdateView in Django, I exclude the user field as it is internal to the system and I won't ask for it. Now what is the proper Django way of passing the user into the form.
(How I do it now, is I pass the user into the init of the form and then override the form's save() method. But I bet that there is a proper way of doing this. Something like a hidden field or things of that nature.
# models.py
class Entry(models.Model):
user = models.ForeignKey(
User,
related_name="%(class)s",
null=False
)
name = models.CharField(
blank=False,
max_length=58,
)
is_active = models.BooleanField(default=False)
class Meta:
ordering = ['name',]
def __unicode__(self):
return u'%s' % self.name
# forms.py
class EntryForm(forms.ModelForm):
class Meta:
model = Entry
exclude = ('user',)
# views.py
class UpdateEntry(UpdateView):
model = Entry
form_class = EntryForm
template_name = "entry/entry_update.html"
success_url = reverse_lazy('entry_update')
#method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(UpdateEntry, self).dispatch(*args, **kwargs)
# urls.py
url(r'^entry/edit/(?P<pk>\d+)/$',
UpdateEntry.as_view(),
name='entry_update'
),
Hacking around like passing a hidden field doesn't make sense as this truly has nothing to do with the client - this classic "associate with logged in user" problem should definitely be handled on the server side.
I'd put this behavior in the form_valid method.
class MyUpdateView(UpdateView):
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = self.request.user
super(MyUpdateView, self).save(form)
# the default implementation of form_valid is...
# def form_valid(self, form):
# self.object = form.save()
# return HttpResponseRedirect(self.get_success_url())
Must return an HttpResponse object. The code below works:
class MyUpdateView(UpdateView):
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = self.request.user
return super(MyUpdateView, self).form_valid(form)
We can also do like
class MyUpdateView(UpdateView):
form_class = SomeModelForm
def form_valid(self, form):
form.instance.user = self.request.user
return super(MyUpdateView, self).form_valid(form)