django-forms

Retrieving models from form with ModelMultipleChoiceField

I am having difficulties with forms, specifically ModelMultipleChoiceField. I've pieced together this code from various examples, but it sadly doesn't work. I would like to be able to: Search for some Works on work_search.html Display the results of the search, with checkboxes next to each result Select the Works I want, via the che...

django - dynamic form fieldsets

A form will be spitting out an unknown number of questions to be answered. each question contains a prompt, a value field, and a unit field. The form is built at runtime in the formclass's init method. edit: each questions receives a unique prompt to be used as a label, as well as a unique list of units for the select element. this s...

How to clean form data depending on logged-in user in django admin panel?

I've looked at several questions here that looked similar, but none of them discussed the problem from the perspective of admin panel. I need to check if user has permission to leave a field empty. I wanted to use request.user but I don't knot how to pass request from EntryAdmin to ModelForm. I wanted to do something like this: class E...

Looking for a good example usage of get_or _create in Django views and raising a Form error

I am looking for a good example of how to achieve the following: I would like to use get_or_create to check whether an object already exists in my database. If it does not, then it will be created. If it does exist, then I will not create the new object, but need to raise a form error to inform the user that they need to enter different...

Get the currently saved object in a view in Django

Hi, I has a Django view which is accessed through an AJAX call. It's a pretty simple one — all it does is simply pass the request to a form object and save the data. Here's a snippet from my view: form = AddSiteForm(request.user, request.POST) if form.is_valid(): obj = form.save(commit=False) obj.user = request.user obj.sav...

Django file uploads - Just can't work it out

OK I give up - after 5 solid hours trying to get a django form to upload a file, I've checked out all the links in stackoverflow and googled and googled. Why is it so hard, I just want it to work like the admin file upload? So I get that I need code like: if submitForm.is_valid(): handle_uploaded_file(req...

How to manually render a Django template for an inlineformset_factory with can_delete = True / False

I have an inlineformset with a custom Modelform. So it looks something like this: MyInlineFormSet = inlineformset_factory(MyMainModel, MyInlineModel, form=MyCustomInlineModelForm) I am rendering this inlineformset manually in a template so that I have more control over widgets and javascript. So I go in a loop like {% for form in myfo...

Editting ForeignKey from "child" table

I'm programming on py with django. I have models: class Product(mymodels.Base): title = models.CharField() price = models.ForeignKey(Price) promoPrice = models.ForeignKey(Price, related_name="promo_price") class Price(mymodels.Base): value = models.DecimalField(max_digits=10, decimal_places=3) taxValue = models.Deci...

Django: Display UTC timestamp field as DateField inside form

My model has a field of integer type representing timestamp in UTC. I would like that field to be displayed as DateField inside my form. I'm using admin interface, i.e I have MyAdmin class inheriting from admin.ModelAdmin, which get passed into admin.site.register(...) function. ...

Django formsets problem.

Hi, I am trying to produce a template with many addresses (forms), where you can add, edit and remove them. Am I doing something wrong with formsets? Here are my views: @login_required def addresses(request): AddressesFormset = modelformset_factory(Address, can_delete = True, ...

Change user email in Django without using django-profiles

Hi guys, In my Django application I would like the user to be able to change the email address. I've seen solution of StackOverflow pointing people to django-profiles. Unfortunately I don't want to use a full fledged profile module to accomplish a tiny feat of changing the users email. Has anyone seen this implemented anywhere. The emai...

MultiWidget in MultiWidget how to compress the first one?

I have two MultiWidget one inside the other, but the problem is that the MultiWidget contained don't return compress, how do i do to get the right value from the first widget? In this case from SplitTimeWidget class SplitTimeWidget(forms.MultiWidget): """ Widget written to split widget into hours and minutes. """ def __i...

How to detect Browser type in Django ?

How can i detect which browser type the client is using. I have a problem where i have to ask people to use different browser (Firefox) instead of IE. How can i get this information. I know http request has this information (Header). How will i get the navigator.appName from the view.py in the Django framework ? ...

Django-subclassed ModelChoiceField Encapsulation

I'm trying to write a Django-subclassed ModelChoiceField class that knows how to load itself, but I'm not having much luck figuring out the pattern required. I don't want form classes that uses my field to have to set anything other than the label and required attributes; that is, I don't want to put duplicate queryset logic in every on...

In django changing the file name of uploading file.

Hi Is it possible to change the file name of the uploading file in the django? I searched, but couldn't find any answer. My requirement is whenever a file is uploaded its file name should be changed in the following format. format = userid + transaction_uuid + file_extension Thank yo very much... ...

Django. Acces request object from admin's form.clean()

My question is very similar to this one: http://stackoverflow.com/questions/1057252/django-how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clea Except, I have the same problem with admin form. So I can't see a way to init the form myself, therefore - to pass a request to it. Thanks beforehand. ...

Django Show M2M field in either model when using forms.ModelForm

Hi I am using the forms.ModelForm to create my form. I want to be able to show the manytomany field in both model forms, how do I do this? If the manytomany relationship is defined in the model it is fine and just appears but if it is not in the model (but is still linked via the other model) it does not appear. How can I make it sh...

How to do custom display and auto-select in django admin multi-select field?

I'm new to django, so please feel free to tell me if I'm doing this incorrectly. I am trying to create a django ordering system. My order model: class Order(models.Model): ordered_by = models.ForeignKey(User, limit_choices_to = {'groups__name': "Managers", 'is_active': 1}) in my admin ANY user can enter an order, but ordered_by mu...

MultipleHiddenInput doesn't encode properly over POST?

The form looks very simple: class MyForm(forms.Form): ids = forms.MultipleChoiceField(widget=forms.MultipleHiddenInput()) def view(request): ... form = MyForm(initial={'ids': [o.id for o in queryset]}) Which gives me the HTML (which looks good enough): <form method="post" action="/foo/bar/"> <input type="hidden" name="ids" ...

Django Fileupload With Email

I am trying to attach a file with a contact form. My code looks like this for the form and view: if request.method == 'POST': form = UploadCVForm(request.POST, request.FILES) if form.is_valid(): # All validation rules pass subject = "CV Sent from BiztechAfrica" sender = form.cleaned_data['email'] message ...