Django dynamic form & ordering of fields - django

I have a form for logging a 'ticket' to a department.
It is a dynamic form which has additional custom fields depending on the form category/department.
Each ticket has standard fields such as title, date, content. Some have fields called custom_acbdef which allows department to ask additional questions on their forms.
These additional fields always appear at the bottom of the form which is OK at the moment. (I add the model form then just loop trough additional fields and add them to self.fields)
Now, I want to add an additional standard field called 'PDF attachment' but I want this to always appear at the bottom of the form. The problem at the moment is all standard fields appear at the top and custom fields appear at the bottom.
class Meta:
model = Ticket
fields = ('ticket_category','ticket_branch','ticket_content', 'ticket_attachment1')
So in the above, I'd want all to insert all my custom fields inbetween ticket_content and ticket_attachment. Any ideas how I can do this? All the custom form fields have dynamic field names but always start with 'custom_'

When things start to become unmanageable inside my forms __init__, I generally take one of the following approaches:
Create a Factory
Leveraging closures, write a function to build out the fields dynamically then return that class.
def TicketForm():
fields = ['title', 'date', 'content']
for custom_field in custom_fields:
fields.append(custom_field)
fields.append('ticket_content')
fields.append('ticket_attachment1')
class _TicketForm(forms.ModelForm):
class Meta:
model = Ticket
fields = fields
return _TicketForm
Multiple forms
I'll create several different forms based on the use case, then within my view determine which one should be returned. I posted an example of this yesterday.
For further reading, check out a post by James Bennett (django core dev) regarding dynamic forms.

I know it's an old post but recently has been published a
package that fulfills this purpose.
It allows to define new fields on-the-fly and populate them in forms and database in a very simple way.
Here is the link
dinadata by undersat package

Related

Django way for dynamic forms with dependencies?

I'm looking for a django way to handle some complex forms with a lot of business logic. The issue is many of my forms have dependencies in them.
Some examples:
1. two "select" (choice) fields that are dependent on each other. For example consider two dropdowns one for Country and one for City.
2. A "required-if" rule, i.e set field required if something else in the form was selected. Say if the user select "Other" option in a select field, he need to add an explanation in a textarea.
3. Some way to handle date/datetime fields, i.e rules like max/min date?
What I'm doing now is implementing all of these in the form clean(), __init__(), and write some (tedious) client-side JS.
I wonder if there is a better approach? like defining these rules in a something similar to django Meta classes.
I'm going to necro this thread, because I don't see a good answer yet. If you are trying to validate a field and you want that field's validation to depend on another field in that same form, use the clean(self) method.
Here's an example: Say you have two fields, a "main_image" and "image_2". You want to make sure that if a user uploads a second image, that they also uploaded a main image as well. If they don't upload an image, the default image will be called 'default_ad.jpg'.
In forms.py:
class AdForm(forms.ModelForm):
class Meta:
model = Ad
fields = [
'title',
'main_image',
'image_2',
'item_or_model_names',
'category',
'buying_or_selling',
'condition',
'asking_price',
'location',
]
def clean(self):
# "Call the cleaned form"
cleaned_data = super().clean()
main_image = cleaned_data.get("main_image")
image_2 = cleaned_data.get("image_2")
if "default_ad" not in image_2:
# Check to see if image_2's name contains "default_ad"
if "default_ad" in main_image:
raise forms.ValidationError(
"Oops, you didn't upload a main image."
)
If you want more info, read: https://docs.djangoproject.com/en/2.2/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other
Good luck!
1.This task is souly related the the html building of the form, not involving django/jinga.
2.Here, you go to dynamic forms. the best and most used way nowdays to do this, is through JS.
3.try building a DB with a "time" type field and then through "admin" watch how they handle it. all of special fields useage is covered here: https://docs.djangoproject.com/en/1.9/ref/forms/fields/

Filtering a ManyToMany field to User insida a form in Django

I've got a custom Model called Group with two ManyToMany fields called admins and members (both to the default User model). I've built a form to edit the administrators:
class AddAdminsForm(forms.ModelForm):
class Meta:
model = Group
fields = (
'admins',
)
Now I want to filter the selection options inside the admins field based on whether it is present in the members field. What would be the best way to achieve that? By entirely changing my members/admins architecture or by somehow messing with the __init__ method inside the form class?
I'm not able to check this solution right now but maybe you can:
....
in your function
form.fields['admins'].queryset = Members.objects.all()

django generic view update/create: update works but create raises IntegrityError

I'm using CreateView and UpdateView directely into urls.py of my application whose name is dydict. In the file forms.py I'm using ModelForm and I'm exluding a couple of fields from being shown, some of which should be set when either creating or updating. So, as mentioned in the title, update part works but create part doesn't which is obvious because required fields that I have exluded are sent empty which is not allowed in my case. So the question here is, how should I do to fill exluded fields into the file forms.py so that I don't have to override CreateView?
Thanks in advance.
Well, you have to set your required fields somewhere. If you don't want them to be shown or editable in the form, your options are to set them in the view (by using a custom subclass of CreateView) or if appropriate to your design in the save method of the model class. Or declare an appropriate default value on the field in the model.
It would also work to allow the fields into the form, but set them to use HiddenInput widgets. That's not safe against malicious input, so I wouldn't do that for purely automated fields.
You cannot exclude fields, which are set as required in the model definition. You need to define blank=True/null=True for each of these model fields.
If this doesn't solve your issue, then please show us the model and form definitions, so we know exactly what the code looks like.

limit choices for foreign field based on other field

Is it possible to do really dynamic form in AdminModel? I have following models:
class MyModel(models.Model):
firstfield=models.ForeignKey(First)
secondField= models.ForeignKey(Second, blank=True,null=True)
#some other fields
class Second(models.Model):
firstfield=models.ForeignKey(First)
#other fields
As you can see Second is optional. But I want it to limit according to current selection in First? It would require some page refreshing or some ajax work but I simply don't know how to even pass First value. Maybe I should add it to request and then use something similar to:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey ?
You can do it through ajax request. If you don't know how it works see the below links.
How to implement two dropdowns dependent on each other using Django and jQuery
Dynamic select fields with JQuery and django

Hidden field in Django Model

A while back I made a Model class. I made several ModelForms for it and it worked beautifully.
I recently had to add another optional (blank=True, null=True) field to it so we can store some relationship data between Users. It's essentially a referral system.
The problem is adding this new field has meant the referral field shows up where I haven't changed the ModelForms to exclude it. Normally this would just mean an extra 10 minutes going through and excluding them but in this case, due to project management politics out of my control, I only have control over the Models for this application.
Can I either:
Set the field to auto-exclude?
Set it so it renders as a hidden (acceptable if not perfect)?
If you have access to the template you could render it as a hidden field with the following code:
{{ form.field_name.as_hidden }}
instead of the standard:
{{ form.field_name }}
from the docs on Using a subset of fields on the form:
Set editable=False on the model field. As a result, any form created from the model via ModelForm will not include that field.
You could define a custom model field subclass and override the formfield() method to return a field with a HiddenInput widget. See the documentation for custom fields.
Though you mentioned that you cannot use exclusion in your case, I think others who come across this answer (like myself, based on the title) may find it helpful.
It is possible to selectively hide fields using exclude in ModelAdmin, here is a snippet from something I'm working on:
class ItemsAdmin(admin.ModelAdmin):
form = ItemsForm
actions = None
list_display = ('item_id', 'item_type', 'item_title', 'item_size', 'item_color',)
search_fields = ('item_id', 'item_title',)
inlines = [ImageInline,]
readonly_fields = ('disable_add_date','disable_remove_date',)
exclude = ('add_date', 'remove_date',)
###.............