django-formsets

Django: How do I make fields non-editable by default in an inline model formset?

I have an inline model formset, and I'd like to make fields non-editable if those fields already have values when the page is loaded. If the user clicks an "Edit" button on that row, it would become editable and (using JavaScript) I would replace the original widgets with editable ones. I'd like to do something like this when loading t...

How to access a forms instance in a modelformset django

In my view I create a formset of photos belonging to a specific article, this works brilliantly, and I am able to render and process the forms. However for the image field I would like to display the already uploaded image. Normally I would access the path through the instance form.instance.image.get_thumbnail_url however that doesn't wo...

Django forset populates with different objects when saving.

I display a formset, which has Forms populated by the objects returned by a queryset. The problem is that before the user submits items are modified or added, such that the queryset returns objects in a different order. The formset then associates each form with the wrong object, users report that their data is shifting down by 1 row. Is...

Django Formsets - form.is_valid() is False preventing formset validation

I'm am utilizing a formset to enable users subscribe to multiple feeds. I require a) Users chose a subscription by selecting a boolean field, and are also required to tag the subscription and b) a user must subscribe to an specified number of subscriptions. Currently the below code is capable of a) ensuring the users tags a subscription...

Customize HTML Output of Django ModelForm

I am trying to add multiple inline form items to a page using Djangos ModelForms. I need Select boxes bound to database models. The forms are formatted and placed in a tabular format so I need to display only the ModelForm without ANY surrounding HTML. class LeagueForm(ModelForm): league = forms.ModelChoiceField(queryset=League.objects....

How do you make a Django Form for a model and all of its children following a particular foreign key?

For example, lets say you want to create a ModelForm for Supervisor that also allows you to create or update 3 or more Underlings in the same form. from django.db import models class Supervisor(models.Model): name = models.CharField(max_length=100) class Underling(models.Model): supervisor = models.ForeignKey(Superisor, related...

Complex forms in Django - what apps and Django/Python features should I look at?

There are a lot of complex forms in my project and I keep getting the feeling that I could be coding them much more elegantly and simply. So my question is what are some good apps and practices that might help me? Specifically, I'm thinking about situations when I need to do stuff like: edit/add more than one object via one form (examp...

Associate and display additional data with each form row in a Django formset

I'd like to be able to display additional information like a text label with each row in a Django formset. Example/usecase: User picks 5 rows in a model that would he would like some action performed. A popup appears that displays a 5 form formset some additional information (that are model instance methods) based on the 5 rows chosen...

Adding new forms to a Django formset based on POST data

I am trying to populate a Django formset using data from a POST request. I am able to display the formset properly and instantiate it again mostly alright, but I'm having an issue where the formset is ending up with 2 blank rows at the end, which is undesirable. Below is the function we've been using in views.py: forms.py class Depos...

django formset - how to update an object

Hi, How can I update an object from a formset using request.POST? Here is my code and my problem is that this always creates a new PhoneNumber object. But I want to update the old PhoneNumber object. def contact_detail(request, contact_id): contact = get_object_or_404(Contact, pk=contact_id) phone_number_list = PhoneNumber.ob...

Django admin, filter objects for inline formset

I've got an inline formset and I would like to exclude some model objects from being displayed in the formset. For eg. there is model B which has foreign key to model A, so it is a 1:n (A object has many B objects) relationship. Now on A admin edit page I've got inlines of B. I wonder if it is possible somehow to filter the list of B o...

Django: Displaying formset errors correctly

I have an inline formset for a model, which has a unique_together constraint. And so, when I input data, which doesn't fulfill this constraint, it displays: __all__Please correct the duplicate values below. The code, which does this is: {% for error in formset.errors %} {{ error }}<br/> {% endfor %} I don't much like...

Django Admin: Customizing the inline template (tabular.html)

I'm trying to follow the guidelines in this answer, but I'm getting stuck with how to edit the template. The relevant part of my admin.py: SegmentFormset = forms.models.inlineformset_factory(Division,Segment) class DivisionForm(forms.ModelForm): def __init__(self, **kwargs): super(DivisionForm, self).__init__(**kwargs) ...

Django-How to use multiple formsets in a single view?

I need to use multiple formsets using custom forms for a single view. I understand that you need to prefix the formsets so that they don't conflict with each other. However, when I try to prefix the formsets, a ValidationError gets raised. The way I'm creating the formsets is by passing a list of forms to the view, then making a list of...

Horizontal (per-row) forms in a Django formset

What's the Django way of presenting a formset horizontally, i.e. one row per form? The as_table method generates multiple forms vertically (with the labels). I need the form fields in table rows (one row per form) and the labels should be on top. I don't see anything out of the box. Is this discouraged for some reason? I should clari...

Django admin different inlines for change and add view

Hi, I need separate views for add and change page. In add page I'd like to exclude some fields from inline formset. I've prepared two TabularInline classes, one of them contains property 'exclude'. I tried to use them as follows: class BoxAdmin(admin.ModelAdmin): def change_view(self, request, obj_id): self.inlines=[ItemCha...

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...

Limit Django's inlineformset_factory to only create new objects

I am using django's inline formset factory. To use the example in the docs, author = Author.objects.get(pk=1) BookFormSet = inlineformset_factory(Author, Book) formset = LicenceFormSet(request.POST, instance=author) will create an inline formset to edit books by a particular author. I want to create a formset that only allows users ...

Django - Working with multiple forms

Hi everybody! What I'm trying to do is to manage several forms in one page, I know there are formsets, and I know how the form management works, but I got some problems with the idea I have in mind. Just to help you to imagine what my problem is I'm going to use the django example models: from django.db import models class Poll(model...

Django formsets: make first required?

These formsets are exhibiting exactly the opposite behavior that I want. My view is set up like this: def post(request): # TODO: handle vehicle formset VehicleFormSetFactory = formset_factory(VehicleForm, extra=1) if request.POST: vehicles_formset = VehicleFormSetFactory(request.POST) else: vehicles_formset ...