django-forms

RESTful multi-step / multi-page forms in Django (ideas/examples)

What are some RESTful ways to transition between pages of a multi-page form in Django? My current method: form POSTs to same page form view validates and stores POST data in session form view redirects to the next form page upon successful validation. next form checks if previous data exists. if not, redirects to first form. Are ...

Django Newbie - With a multiple fields form, how do I eliminate empty fields within the queryset

Hello, I'm a Django newbie, so please forgive me if this is a stupid question. I have a search form that has multiple fields on it. I only wish to filter my queryset by those fields submitted that aren't empty. How do I do that? I'm aware you can chain querysets and Q objects together, but I don't know how to eliminate empty key/value p...

Making email field unique with Django User admin.

There was a nearly similar question: http://stackoverflow.com/questions/1160030/how-to-make-email-field-unique-in-model-user-from-contrib-auth-in-django The solution was not perfect: Validating email for uniqueness. The solution provided is rather funny. It disallows modifications to User that leave email intact. How to fix it? Thanks i...

How to prevent Django Admin Users from changing other Admin Users' profile data?

I have Admin User extended/subclassed by Teacher class. How to prevent Teachers from seeing and changing other Teachers' profile data and Teachers are able to change their own records/rows only? Thanks in advance! ...

Adding extra constraints into fields in Django

While subclassing db.models.Model, sometimes it's essential to add extra checks/constraints. E.g. I have an Event model with start_date and end_date. I want to add validation into the fields or the model so that end_date > start_date. How many possible ways to do this? At least I know this can be done outside the models.Model inside the ...

Django: display/edit form depending on whether user is authenticated?

I have a form in Django. If the user is authenticated, I want them to see a form that they can edit: if they aren't, then I would like them to see a display-only form. In both cases, I want to show the same information, just that if they are authenticated then I want the form to be editable. Is there an easy way to do this in Django? ...

Send mail/save draft from Django admin

Hi everybody, I wonder if there is a way to override Django admin's submit_line buttons for a specific model, so that instead of showing the save & save and add another options they show send mail or save draft and actually work... Thanks a million! ...

Django Forms with get_or_create

Hi I am using Django ModelForms to create a form. I have my form set up and it is working ok. form = MyForm(data=request.POST) if form.is_valid(): form.save() What I now want though is for the form to check first to see if an identical record exists. If it does I want it to get the id of that object and if not I want it to in...

Django FormSets as Fields of another Form

I have a Django model that looks like this: DEFAULT_PROFILE_INFO = [ {"name":"About Me", "body":"Super programmer", "public":True}, {"name":"Research", "body":"Various topics", "public":False} ] class Profile(models.Model): user = models.OneToOneField(User, unique=True) info = JSONField(blank=True, default=DEFAULT_PROFI...

Django's forms.Form vs forms.ModelForm

Could anyone please explain to me similarities and differences of Django's forms.Form & forms.ModelForm? I've used Django for 4 days, so excuse me if I don't know basic things. Thanks in advance! ...

django quantity form widget

Hello, I want to develop a basic quantity widget that is a dropdown selection box, consuming an integer which will be the maximum amount of quantity, users can select from 1 to the maximum quantity. And in the end my form will be using this widget and if somehow the given amount is greater than the maximum, it shouldn't validate. (indee...

Django form validation: making "required" conditional?

I'm new to Django (and Python), and am trying to figure out how to conditionalize certain aspects of form validation. In this case, there's a HTML interface to the application where the user can choose a date and a time from widgets. The clean method on the form object takes the values of the time and date fields and turns them back into...

Django, validating Form

Hello, I want my forms to be dynamic, some parameters sucj as max_length for IntegerField varies from model to model. By using the information from here, I have written this form: def my_form(inputname, inputlastname, inputamount): class MyForm(forms.Form): name = forms.CharField(max_length=50,required=True,initial=inputname) last...

Django - Overriding the Model.create() method?

The Django docs only list examples for overriding save() and delete(). However, I'd like to define some extra processing for my models only when they are created. For anyone familiar with Rails, it would be the equivalent to creating a :before_create filter. Is this possible? ...

django inlineformset_factory: how to pass arguments to form init method

Hi, I'm using inlineformset_factory passing the form to be used in the form argument (form=ProjectMembershipWorkPlaceDayRateForm): PMWPDRFormSet = inlineformset_factory(ProjectMembership, PMSWorkPlaceDayRate, form=ProjectMembershipWorkPlaceDayRateForm, max_num=10, extra=2, can_delete = False) I'm wondering whether there is a way to p...

Django outputs CSRF token as object instead of value

Hi, I am struggling with the CSRF token in a simple POST form in Django. The template generates the following CSRF output instead of outputting the value of the token: <input type='hidden' name='csrfmiddlewaretoken' value='{'csrf_token':django.utils.functional.__proxy__ object at 0x1255690>}' /> I am using {% csrf_token %} in the ...

In django forms custom widget return list as value instead of string

Hi I am writting a custom widget which I want to return a list as the value. From what I can find to set the value that is returned you create a custom value_from_datadict function. I have done this def value_from_datadict(self, data, files, name): value = data.get(name, None) if value: # split the sting up so that we...

django form field - how to present the value in a form template

Hi, I have a model with a FileField which is shown below. I use this model in a model form. receipt = models.FileField(upload_to='receipt/%m-%Y/', max_length=255) I can save the object and the receipt field contains the file url. Now when I present this object with the model form the file url is not presented, instead a message "no...

InlineFormSet - Get a User ModelForm when the relationship is in the User.Profile model

My end result is to provide a User Permissions Matrix. (django.contrib.auth.models.Permission) Working with three models in this scenario: Office, User, and UserProfile. Users are related to an Office model through the UserProfile model. See below for my model setup: class User(models.Model): # the included django User model class Use...

Can the inlineformset_factory create a tabularInline?

I have activity records that have an inline formset showing appointments. I use the inlinefomset_factory. It works but it displays them as an equivalent to a StackedInline you find in InlineModelAdmin. However, I wondered if it can product tabularInline or if anyone has done anything similar? ...