I want to create something similar as django admin changelist view with list_editable items...
I succeeded in creating the view. But when I post it dies on validation errors.
if request.POST:
formset_class = modelformset_factory(Job)
formset =formset_class(request.POST, request.FILES)
if formset.is_valid():
formset.save()
The problem is that I have only a couple of attributes editable. Therefore some of them are not part of POST and model complains about them being mandatory.
But I want to UPDATE objects not create them. Basically I really want the same thing admin does when having set list_editable but in my own view
I wanted a functionality just like admin using list_editable, so I went for it and pretty much copied the code from options.py of django source. I retrieved admin for my object and then saved original values (function fix_old_job_admin sets them back)
This code solved my problem
job_admin = admin.site._registry[Job]
# save old values so that you can go back to them later
old_list_display = job_admin.list_display
old_list_filter = job_admin.list_filter
old_ordering = job_admin.model._meta.ordering
job_admin.list_editable = ("time", "what", "approved")
cl = ChangeList(request, job_admin.model, job_admin.list_display, job_admin.list_display_links, job_admin.list_filter, job_admin.date_hierarchy, job_admin.search_fields, job_admin.list_select_related, job_admin.list_per_page, job_admin.list_editable,job_admin.admin_site, job_admin)
# options.py from django framework lines 1181-1208 (v. 1.4)
if request.POST:
FormSet = job_admin.get_changelist_formset(request)
formset =FormSet(request.POST, request.FILES, queryset=cl.result_list)
if formset.is_valid():
changecount = 0
for form in formset.forms:
if form.has_changed():
obj = job_admin.save_form(request, form, change=True)
job_admin.save_model(request, obj, form, change=True)
job_admin.save_related(request, form, formsets=[], change=True)
change_msg = job_admin.construct_change_message(request, form, None)
job_admin.log_change(request, obj, change_msg)
changecount += 1
if changecount:
if changecount == 1:
name = force_unicode(job_admin.model._meta.verbose_name)
else:
name = force_unicode(job_admin.model._meta.verbose_name_plural)
msg = ungettext("%(count)s %(name)s was changed successfully.",
"%(count)s %(name)s were changed successfully.",
changecount) % {'count': changecount,
'name': name,
'obj': force_unicode(obj)}
job_admin.message_user(request, msg)
# call function that sets admin with original values
fix_old_job_admin(job_admin, old_list_display, old_ordering, old_list_filter)
return HttpResponseRedirect(request.get_full_path())
FormSet = job_admin.get_changelist_formset(request)
cl.formset = FormSet(queryset=cl.result_list)
context = Context({
'app_label': ContentType.objects.get_for_model(Lawyer).app_label,
'verbose_name_plural': Job._meta.verbose_name_plural.title(),
"cl": cl,
'request': request,
})
# call function that sets admin with original values
fix_old_job_admin(job_admin, old_list_display, old_ordering, old_list_filter)
return render_to_response('yourtemplate/similar_to_changelist.html', context, RequestContext(request))
Related
I wish I could make a loop that would allow me to instantiate all my formsets for now I have written
def access(request, page_id):
if request.method == 'POST':
formset = ReplyFormSet(request.POST, request.FILES, initial=[{'instance':instance,}])
if formset.is_valid():
#...
return ...
else:
formset = ReplyFormSet(queryset=Reply.objects.filter(question=questions))
return ...
On this last line that I recovered the information in the db but just for my first forms! Is there way to make a loop so that it instantiates me all the forms?
I tried something like this but it does not work :
else:
for form in formset:
form.formset = ReplyFormSet(queryset=Reply.objects.filter(question=questions))
return ...
This line only works for my first form :
formset = ReplyFormSet(queryset=Reply.objects.filter(question=questions))
How can I do to affect it at all?
Should I change my init function of my ReplyForm ?
You could try:
formset = ReplyFormSet(queryset=Reply.objects.filter(question__in=questions))
Using the in filter you get every question which is in questions queryset.
I am not understanding why, but an instance always get created copying the one I am trying to edit. Also, as I can see FormSet I am using does not have an "instance" parameter to be added to its constructor. Anyways, the problem is that an instance of both Offer and OfferItem gets generated when I am editing an object.
def manage_offer(request, number=None):
param_offer = Offer.objects.filter(id=number).first()
param_items = OfferItem.objects.filter(offer=param_offer).values()
if request.method == 'POST':
offer_form = OfferForm(request.POST, instance=param_offer)
item_formset = OfferItemFormSet(request.POST, initial=param_items)
if offer_form.is_valid() and item_formset.is_valid():
# User selected go back and correct something
if request.POST.get('back', False):
return render(request,
'offer_edit.html',
{
'forms': offer_form,
'formset': item_formset,
'offer_edit': True,
})
# Proceeds with either saving or submitting request
offer = offer_form.save(commit=False)
offer.tax = offer_form.cleaned_data['tax'].value
#Sotres items to be sent back to commit part
offer_items = []
#Gets the items from the form and stores them to conf. page
for item_in_formset in item_formset.forms:
item = item_in_formset.save(commit=False)
item.item_code = get_item_code(item_in_formset.cleaned_data['name'])
item.type = get_item_type(item_in_formset.cleaned_data['name'])
offer.update_total(item.calc_total())
# Adds items into list for invoice_ready page
offer_items.append(item)
# Request goes to confirmation page
if request.POST.get('proceed', False):
return render(request,
'offer_edit.html',
{
'offer_form': offer_form,
'item_formset': item_formset,
'offer_ready': True,
'offer': offer,
'items': offer_items,
})
# Passes confirmation page and saves offer
offer.save()
# Makes sure the value is correct by recalculating
offer.reset_total()
for obj_item in offer_items:
obj_item.offer = offer
offer.update_total(obj_item.calc_total())
#commits to DB
offer.save()
obj_item.save()
return render(request,
'offer_edit.html',
{
'success_add_offer': True,
'offer': offer,
},
)
# GET generates a blank or instanced page
else:
offer_form = OfferForm(initial=
{'company': Company.objects.filter(is_default=True).first(),
'tax': Tax.objects.filter(is_default=True).first()
}, instance=param_offer)
item_formset = OfferItemFormSet(initial=param_items)
return render(request, 'offer_edit.html',
{
'forms': offer_form,
'formset': item_formset,
'edit_offer': number,
})
Forms.py
class OfferItemForm(ModelForm):
class Meta:
model = OfferItem
# Some widgets and stuff ...
class RequiredFormSet(BaseFormSet):
def __init__(self, *args, **kwargs):
super(RequiredFormSet, self).__init__(*args, **kwargs)
for form in self.forms:
form.empty_permitted = False
OfferItemFormSet = formset_factory(OfferItemForm, formset=RequiredFormSet)
I found the problem: since I am using the same View function to either edit or add a new entry, on my template form I must make sure I am also identifying if I am editing or not, because the function takes a parameter in case I am editing. In that case I must change the URL in the Post form.
Just a silly mistake take took me a few hours to find out.
def itemconfirmation(request, pk):
item = Food_item.objects.get(id=pk)
userobj = request.user
user = UserProfile.objects.get(user=userobj)
if request.method == 'POST':
count_form = CountForm(data=request.POST)
if count_form.is_valid():
countform = count_form.save(commit=False)
countform.useradno = user.adno
countform.itemid = item.id
countform.save()
c = RequestContext(request, {
'item': item, 'count_form': count_form
})
return render_to_response('itemconfirmation.html', context_instance=c)
I have a view defined like this. I'm getting error in making the user object extended to UserProfile and cannot user the user.id
Is the request.user authenticated? is it an AnonymousUser?
UserProfile.objects.get() method is not for creating an object but to get it from the database.
if it doesn't exist an exception will be raised.
use UserProfile.objects.create(..) with the initial data you may need for it.
hope this helps!
== edit ==
also, note that you are referring count_form in the RequestContext even when it wasn't initialized in case that the request.method was not "POST" (i.e. "GET")
I currently have a model form that submits an entered domain to the db.
The problem I'm encountering is, I need to save the currently logged in user's ID (PK from the django.auth table) when a domain is submitted to satisfy a PK-FK relationship on the db end.
I currently have:
class SubmitDomain(ModelForm):
domainNm = forms.CharField(initial=u'Enter your domain', label='')
FKtoClient = User.<something>
class Meta:
model = Tld #Create form based off Model for Tld
fields = ['domainNm']
def clean_domainNm(self):
cleanedDomainName = self.cleaned_data.get('domainNm')
if Tld.objects.filter(domainNm=cleanedDomainName).exists():
errorMsg = u"Sorry that domain is not available."
raise ValidationError(errorMsg)
else:
return cleanedDomainName
and views.py
def AccountHome(request):
if request.user.is_anonymous():
return HttpResponseRedirect('/Login/')
form = SubmitDomain(request.POST or None) # A form bound to the POST data
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
domainNmCleaned = form.cleaned_data['domainNm'] ## clean data in dictionary
clientFKId = request.user.id
form.save() #save cleaned data to the db from dictionary`
try:
return HttpResponseRedirect('/Processscan/?domainNm=' + domainNmCleaned)
except:
raise ValidationError(('Invalid request'), code='300') ## [ TODO ]: add a custom error page here.
else:
form = SubmitDomain()
tld_set = request.user.tld_set.all()
return render(request, 'VA/account/accounthome.html', {
'tld_set':tld_set, 'form' : form
})
The problem is it gives me an error of: (1048, "Column 'FKtoClient_id' cannot be null"), very odd thing happening, for the column FKtoClient, its trying to submit: 7L instead of 7(the PK of this user's record). Any ideas?
If someone can please help, I would really appreciate it
Firstly, remove FKtoClient from your form. You need to set the user in your view where you can yes the request object. It's not possible to set an attribute on the form that automatically sets the current user.
When instantiating your form, you can pass a tld instance which already has the user set.
def AccountHome(request):
# I recommend using the login required decorator instead but this is ok
if request.user.is_anonymous():
return HttpResponseRedirect('/Login/')
# create a tld instance for the form, with the user set
tld = Tld(FKtoClient=request.user)
form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance
if request.method == 'POST': # If the form has been submitted...
if form.is_valid(): # If form input passes initial validation...
domainNm = form.cleaned_data['domainNm']
form.save() #save cleaned data to the db from dictionary
# don't use a try..except block here, it shouldn't raise an exception
return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
# No need to create another form here, because you are using the request.POST or None trick
# else:
# form = SubmitDomain()
tld_set = request.user.tld_set.all()
return render(request, 'VA/account/accounthome.html', {
'tld_set':tld_set, 'form' : form
})
This has an advantage over #dm03514's answer, which is that you can access the user within form methods as self.instance.user if required.
If you want to Require that a user be logged in to submit a form, you could do something like:
#login_required # if a user iS REQUIRED to be logged in to save a form
def your_view(request):
form = SubmitDomain(request.POST)
if form.is_valid():
new_submit = form.save(commit=False)
new_submit.your_user_field = request.user
new_submit.save()
You can get the logged in user from the request object:
current_user = request.user
I'm using django_tables2 thus part of the code is dependent on this package, but this should be irrelevant to the overall problem.
forms.py
class PersonForm(forms.Form):
date_from = forms.DateField(input_formats='%d/%m/%Y')
date_to = forms.DateField(input_formats='%d/%m/%Y')
views.py
def filter(request):
table = PersonTable(Person.objects.all())
if request.method == 'POST':
form = PersonForm(request.POST)
if form.is_valid():
date_from = form.cleaned_data['date_from']
date_to = form.cleaned_data['date_to']
result_filtered = table.filter(date__range=(date_from, date_to))
RequestConfig(request, paginate={"per_page": 100}).configure(table)
return render(request, "people.html", {
"table": result_filtered })
args = {}
args.update(csrf(request))
args['form'] = PersonForm()
args['table'] = table
RequestConfig(request, paginate={"per_page": 100}).configure(table)
return render(request, 'people.html', args)
Simply, filtering is not working. I can see the entire table, but when I try to filter nothing happens. Can you see what's wrong?
I'm pretty sure you need to call .filter() on the query set rather than the table. For example:
result_filtered = PersonTable(Person.objects.filter(date__range=(date_from, date_to))
Also, on this line:
RequestConfig(request, paginate={"per_page": 100}).configure(table)
You are passing in table. You should pass in result_filtered instead.
This is one way I'd do it, assuming your Person model has a date field:
def filter(request):
if 'date_from' in request.GET and 'date_to' in request.GET:
# form data has been submitted
form = PersonForm(request.GET)
if form.is_valid():
date_from = form.cleaned_data['date_from']
date_to = form.cleaned_data['date_to']
people = Person.objects.filter(date__range=(date_from, date_to))
table = PersonTable(people)
else:
table = PersonTable(Person.objects.all())
else:
form = PersonForm()
table = PersonTable(Person.objects.all())
RequestConfig(request, paginate={"per_page": 100}).configure(table)
args = {}
args.update(csrf(request))
args['form'] = form
args['table'] = table
return render(request, 'people.html', args)
This binds the form if both its expected fields are present, and limits the queryset according to the results if valid. If not valid, it renders the bound form and the table built from the unlimited table. If form data was not submitted, it renders the table built from the unlimited table.
Your form tag's method attribute should be GET rather than POST, if using this design.
This doesn't quite follow the usual Django patterns for form handling because the form isn't actually making any changes, so you can use GET and don't need to return a redirect on success.