i am developing a simple app with python django framework and i am using Class based Views, when i use the UpdateView and try to run my template i get this error;
'QuerySet' object has no attribute '_meta'
This is my view codes
class UpdateStaff(LoginRequiredMixin, UpdateView):
template_name = 'app/update_staff.html'
form_class = UpdateStaffForm
model = Staff
def get_object(self, queryset=None):
obj = Staff.objects.filter(pk=self.kwargs['staff_id'])
return obj
def post(self, request, *args, **kwargs):
form = self.form_class(request.POST or None,
instance=self.get_object())
if form.is_valid():
obj = form.save(commit=False)
obj.save()
messages.success(self.request, "Staff has been updated")
return self.get_success_url()
else:
messages.error(self.request, "Staff not updated")
return HttpResponseRedirect(reverse('update_staff'))
def get_success_url(self):
return HttpResponseRedirect(reverse('manage_staffs'))
def get_context_data(self, **kwargs):
context = super(UpdateStaff,
self).get_context_data()
context['messages'] = messages.get_messages(self.request)
context['form'] = self.form_class(self.request.POST or None,
instance=self.get_object())
return context
and this is my form codes:
class UpdateStaffForm(ModelForm):
class Meta:
model = Staff
exclude = (
'profile_picture', 'start_work', 'last_login', 'groups',
'user_permissions', 'is_active', 'is_superuser',
'date_joined', 'end_work', 'can_sell_out_of_assigned_area',
'is_staff')
def __init__(self, *args, **kwargs):
super(UpdateStaffForm, self).__init__(*args,
**kwargs)
for field_name, field in self.fields.items():
field.widget.attrs['class'] = 'form-control'
Anyone having an idea to solve this please help.
The get_object method returns queryset i.e list of records, instead of instance.To get instance you can use first() on filter() . This will gives you first occurrence.
def get_object(self, queryset=None):
obj = Staff.objects.filter(pk=self.kwargs['staff_id']).first()
return obj
Late answer but worth it.
from django.shortcuts import get_object_or_404
...
def get_object(self, queryset=None):
obj = get_object_or_404(Staff, pk=self.kwargs['staff_id'])
return obj
This approach will return HTTP 404 Not Found response if object not found.
Related
I am trying to pass the request object into my inlineformset_factory and am struggling to accomplish this.
In forms.py I have the following:
class SummativeScoreForm(forms.ModelForm):
"""
Form definition for SummativeScore Form
"""
subdomain_proficiency_level = forms.ModelChoiceField(
empty_label="Undecided",
queryset=SubdomainProficiencyLevel.objects.none(),
widget=forms.RadioSelect,
required=False,
)
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request", None)
super(SummativeScoreForm, self).__init__(*args, **kwargs)
if self.instance:
if self.request.user == self.instance.summative.employee:
self.fields["subdomain_proficiency_level"].disabled = True
self.fields[
"subdomain_proficiency_level"
].queryset = SubdomainProficiencyLevel.objects.filter(
subdomain=self.instance.subdomain
)
self.fields[
"subdomain_proficiency_level"
].label = f"""
{self.instance.subdomain.character_code}:
{self.instance.subdomain.short_description}
"""
class Meta:
model = SummativeScore
fields = "__all__"
widgets = {
"subdomain_proficiency_level": forms.RadioSelect(
attrs={"class": "list-unstyled"}
),
}
SummativeScoreInlineFormset = inlineformset_factory(
Summative,
SummativeScore,
fields=("subdomain_proficiency_level",),
can_delete=False,
extra=0,
form=SummativeScoreForm,
)
I'm using a FormView CBV to show this inline_formset
class SummativeScoreFormView(
LoginRequiredMixin,
UserIsObserverOrObserveeMixin,
SingleObjectMixin,
FormView,
):
model = Summative
template_name = "commonground/summative_score_form.html"
pk_url_kwarg = "summative_id"
def get(self, request, *args, **kwargs):
summative_id = kwargs.pop("summative_id")
self.object = self.get_object(
queryset=Summative.objects.filter(id=summative_id)
)
return super().get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
summative_id = kwargs.pop("summative_id")
self.object = self.get_object(
queryset=Summative.objects.filter(id=summative_id)
)
return super().get(request, *args, **kwargs)
def get_form(self, form_class=None):
return SummativeScoreInlineFormset(
**self.get_form_kwargs(), instance=self.object
)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs["request"] = self.request
return kwargs
def form_valid(self, form):
form.save()
messages.add_message(messages.SUCCESS, "Changes were saved!")
HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form):
print("invalid form")
return super().form_invalid(form)
def get_success_url(self):
user_id = self.kwargs["user_id"]
summative_id = self.kwargs["summative_id"]
return reverse(
"commonground:summative_detail",
kwargs={
"user_id": user_id,
"summative_id": summative_id,
},
)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
summative = get_object_or_404(
Summative, pk=self.kwargs["summative_id"]
)
context["summative"] = summative
return context
I keep getting this error:
__init__() got an unexpected keyword argument 'request'
I'm not sure how best to resolve this - any ideas? Is there a clear way to pass the request to the inlineformset_factory?
You are passing the request to the formset and not to the forms in the formset, hence you get the error. For passing custom parameters to the formsets forms [Django docs] you need to pass a form_kwargs parameter to the formset:
class SummativeScoreFormView(
LoginRequiredMixin,
UserIsObserverOrObserveeMixin,
SingleObjectMixin,
FormView,
):
...
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['form_kwargs'] = {'request': self.request}
return kwargs
I have a detailed user view that has a button for user updates. The user update form is inside a modal, for that, I am using a FormView ModelForm and a TbUser. I don't get how the form validation works but the fields are correct. When I update something for a user, I get an error, TbUser with username already exists, which means the code does not update the user but tries to add a new one. Also, I want to redirect to user-detail page after submit.
views.py
class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, FormView):
form_class = UserUpdateForm
template_name = 'users/modals/user_update_modal.html'
success_message = "User updated successfully."
def get_form_kwargs(self):
kw = super().get_form_kwargs()
kw['request'] = self.request
return kw
def form_valid(self, form):
obj = form.save(commit=False)
print(obj.username)
print('valid')
TbUser.objects.filter(id=self.request.user.id).update(username=obj.username, real_name=obj.real_name,
email=obj.email, cellphone=obj.cellphone,
department=obj.department, role=obj.role)
def form_invalid(self, form):
messages.error(self.request, form.errors)
# Where to redirect here? I want to
def get_success_url(self):
return reverse('user-detail', kwargs={'pk': self.formclass})
forms.py
class UserUpdateForm(forms.ModelForm):
email = forms.EmailField()
def __init__(self, request, *args, **kwargs):
super().__init__(*args, **kwargs)
self.request = request
if request.user.customer:
self.fields['department'].queryset = TbDepartment.objects.filter(
customer=request.user.customer)
self.fields['role'].queryset = TbRole.objects.filter(
customer=request.user.customer)
class Meta:
model = TbUser
fields = ['username', 'real_name', 'email',
'cellphone', 'department', 'role']
urls.py
urlpatterns = [
path('users-list/', views.UsersListView.as_view(), name='users-list'),
path('user-detail/<str:pk>/',
views.UserDetailView.as_view(), name='user-detail'),
path('tb-user-update-form/<str:pk>/update/',
views.UserUpdateView.as_view(), name='tb-user-update-form'),
]
You need to override the get_object method to let it point to the object you want to update.
A FormView will construct a form *without looking for an object, you can use an UpdateView [Django-doc] to fetch the object with get_object, and then inject this in the form to update that object:
from django.views.generic import UpdateView
class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView):
form_class = UserUpdateForm
template_name = 'users/modals/user_update_modal.html'
success_message = "User updated successfully."
def get_form_kwargs(self):
kw = super().get_form_kwargs()
kw['request'] = self.request
return kw
def get_object(self, *args, **kwargs):
return self.request.user
def form_invalid(self, form):
messages.error(self.request, form.errors)
return super().form_invalid(form)
def get_success_url(self):
return reverse('user-detail', kwargs={'pk': self.object.pk })
I'm fairly new to python and have been searching for awhile to find how I can edit form data BEFORE all the standard python form/field validators do their magic.
I have a model form with an IntegerField which I'd like to remove the "$" and commas from (using some sort of custom validation), then let the normal to_python() validate() etc do their thing.
My code is below - any help would be much appreciated!
forms.py
class BuyerSettingsForm(forms.ModelForm):
total_offer_limit = forms.IntegerField(required=False, max_value=10000000, min_value=0)
def __init__(self, request, *args, **kwargs):
super(BuyerSettingsForm, self).__init__(*args, **kwargs)
class Meta:
model = Buyer
fields = ['total_offer_limit']
def save(self, commit=True):
profile = super(BuyerSettingsForm, self).save(commit=commit)
profile.total_offer_limit = self.cleaned_data['total_offer_limit']
profile.save()
return profile
views.py
class SettingsPreferences(LoginRequiredMixin, BuyerAccessRequiredMixin, BuyerAdminAccessRequiredMixin, UpdateView):
template_name = 'invoicely/buyer/settings/buyer_settings.html'
form_class = BuyerSettingsForm
success_url = reverse_lazy('settings_preferences')
def get_object(self, *args, **kwargs):
return self.request.user.profile.buyer
def get_initial(self):
ctx = super(SettingsPreferences, self).get_initial()
ctx.update({
'total_offer_limit': self.object.total_offer_limit,
})
return ctx
def get_form_kwargs(self):
kwargs = super(SettingsPreferences, self).get_form_kwargs()
kwargs['request'] = self.request
return kwargs
def form_valid(self, form):
self.object = form.save()
messages.add_message(self.request, messages.SUCCESS, "Settings successfully updated")
return super(SettingsPreferences, self).form_valid(form)
If you are already overloaded get_form_kwargs you can do this. This is data which your form will be initialized with. So we can edit it before its initialization.
class SettingsPreferences(LoginRequiredMixin, BuyerAccessRequiredMixin, BuyerAdminAccessRequiredMixin, UpdateView):
...
def get_form_kwargs(self):
kwargs = super(SettingsPreferences, self).get_form_kwargs()
kwargs = copy.deepcopy(kwargs)
kwargs['request'] = self.request
if self.request.method in ('POST', 'PUT'):
# here put your data editing code
kwargs['data']['total_offer_limit'] = int(kwargs['data']['total_offer_limit'].strip().replace('$', ''))
return kwargs
Hello I get this error:
KeyError at /sss/edit/
'pk'
I know KeyError mean there's no key in a dict but I don't understand why it's giving me this error here.
Here's my code:
class PostUpdateView(UpdateView):
model = Post
form_class = PostForm
template_name = 'main/edit.html'
def form_valid(self, form):
self.object = form.save(commit=False)
# Any manual settings go here
self.object.save()
return HttpResponseRedirect(self.object.get_absolute_url())
#method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
post = Post.objects.get(pk=kwargs['pk'])
if request.user.has_perm('main.change_post') and post.created_by == request.user:
return super(PostUpdateView, self).dispatch(request, *args, **kwargs)
else:
return http.HttpForbidden()
I get error from post=Post.objects.get(pk=kwargs['pk'])
I have this model as well
class Post(models.Model):
pub_date = models.DateTimeField(auto_now_add = True)
You can only access the pk if you have defined it in your urls.py like this:
url(r'^sss/edit/(?P<pk>\d+)$', PostUpdateView.as_view())
I need to set the value of a BooleanField from my Django model via the CreateView for my ModelForm. But for some reason, setting it in form_valid() isn't working.
Here's my model:
class Feedback(models.Model):
was_satisifed = models.BooleanField(
help_text='Returns true if the user exits the process early.',
default=False)
Here's my view:
class FeedbackActionMixin(object):
model = Feedback
form_class = FeedbackForm
def form_valid(self, form):
instance = form.save(commit=False)
instance.was_satisfied = True
return super(FeedbackActionMixin, self).form_valid(form)
The form submits, but the "was_satisfied" value is left at the default False. What am I missing?
If memory serves, form_valid is called after the data has already been posted. You might try overriding the post method on your mixin:
class FeedbackActionMixin(object):
model = Feedback
form_class = FeedbackForm
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
form.was_satisfied = True
return self.form_valid(form)
else:
return self.form_invalid(form)
Here's what I ended up doing, though in forms.py not views.py:
class SuccessfulFeedbackForm(FeedbackFormMixin, forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SuccessfulFeedbackForm, self).__init__(*args, **kwargs)
self.fields['was_satisifed'].initial = True
class UnsuccessfulFeedbackForm(FeedbackFormMixin, forms.ModelForm):
def __init__(self, *args, **kwargs):
super(UnsuccessfulFeedbackForm, self).__init__(*args, **kwargs)
self.fields['was_satisifed'].initial = False