I am trying to set the fieldorder of my form. but somehow it just stays in alphabetical order. Anyone has some suggestions? i tried class Meta: fields = ["field", "field"] and adding a keyOrder in the init
form:
class HangarFilterForm(forms.Form):
FIELDS = [
("", ""),
("warp", "Warp"),
("cargo_space", "Cargo Space"),
("smuggle_bay", "Smuggle Bay"),
("dock", "Dock/Undock"),
("enter_warp", "Enter Warp"),
("fuel_bay", "Fuel Bay"),
("fuel_cost", "Fuel Cost"),
]
PER_PAGE = [
(10, ""),
(5, "5 ships"),
(10, "10 ships"),
(25, "25 ships"),
(50, "50 ships"),
]
field_1 = forms.ChoiceField(choices=FIELDS, label="1st attribute", required=False)
field_2 = forms.ChoiceField(choices=FIELDS, label="2nd attribute", required=False)
per_page = forms.ChoiceField(choices=PER_PAGE, required=False)
def __init__(self, *args, **kwargs):
super(HangarFilterForm, self).__init__(*args, **kwargs)
self.fields['planet'] = forms.ChoiceField(
choices=[("", "")] + [ (o.id, o.name) for o in lanet.objects.all().order_by("name")],
required=False)
self.fields['type'] = forms.ChoiceField(
choices=[("", "")] + [ (o[0], o[1]) for o in ShipTemplate.SHIP_TYPES], required=False)
self.fields.keyOrder = ["planet", "type", "field_1", "field_2", "per_page"]
In Django 1.9, new way of forcing the order of form's fields has been added : field_order.
Take a look (link to version 1.9):
https://docs.djangoproject.com/en/1.9/ref/forms/api/#django.forms.Form.field_order
(and a link to dev version):
https://docs.djangoproject.com/en/dev/ref/forms/api/#django.forms.Form.field_order
Find below a short example (using Django 1.9)
models.py:
from django.db import models
class Project(models.Model):
end_date = models.DateField(verbose_name='End date',
blank=True)
start_date = models.DateField(verbose_name='Start date',
blank=True)
title = models.CharField(max_length=255,
blank=False,
verbose_name='Title')
def __str__(self):
return self.title
forms.py
from django.forms import ModelForm, DateTimeField, SelectDateWidget
from XXX.models import Project
class ProjectForm(ModelForm):
class Meta:
model = Project
fields = '__all__'
start_date = DateTimeField(widget=SelectDateWidget)
end_date = DateTimeField(widget=SelectDateWidget)
field_order = ['start_date', 'end_date']
In this example the fields will be rearranged to:
start_date <== using the list in the form class
end_date <== using the list in the form class
title <== not mentioned in the list, thus using the default ordering
I tried setting fields in Meta part of form in django 2.1 and it also did the trick:
class MyForm(forms.ModelForm):
...
class Meta:
model = Contact
fields = ('field_1', 'field_2', 'field_3', 'field_4',)
This is some code that I've done in the past to rearrange the field order in forms that has worked; you could probably put this into a mixin for use elsewhere. Let me know how it goes.
from django.utils.datastructures import SortedDict
class HangarFilterForm(forms.Form):
ordered_field_names = ['planet', 'type', 'field_1', 'field_2', 'per_page']
def __init__(self, *args, **kwargs):
super(HangarFilterForm, self).__init__(*args, **kwargs)
# Your field initialisation code
self.rearrange_field_order()
def rearrange_field_order(self):
original_fields = self.fields
new_fields = SortedDict()
for field_name in self.ordered_field_names:
field = original_fields.get(field_name)
if field:
new_fields[field_name] = field
self.fields = new_fields
If you want to keep track of the original file order for some reason, you can just change original_fields to self.original_fields in rearrange_field_order.
Might be a little bit off topic. Using django crispy forms and their Layout objects can help a great deal with formatting forms the way you exactly want. Which includes rearranging the field order.
A sample to illustrate:
class UpdateUserForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
Field('email'),
Field('quote'),
Field('website', placeholder="http://"),
Field('logo', template="user/logoInput.html"),
HTML('<label class="control-label">Other settings</label>'),
Field('emailVisible'),
Field('subscribeToEmails'),
Field('mpEmailNotif'),
Field('showSmileys'),
Field('fullscreen'),
)
class Meta:
model = ForumUser
fields = ('email', 'emailVisible', 'subscribeToEmails', 'mpEmailNotif',
'logo', 'quote', 'website', 'showSmileys', 'fullscreen')
I used the bspink's way with some improvements. You don't need to define the fields that you don't want to change their ordering. Define only what you want to put to the top as ordered in themself.
(Be sure you are using the Python 3.7+)
class SomeForm(forms.Form):
# define only you want to put top, no need to define all of them
# unless you need more specific ordering
ordered_field_names = ['planet', 'type']
def __init__(self, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
# call initialization code
self.rearrange_field_order()
def rearrange_field_order(self):
# add defined fields first
new_fields = {field_name: self.fields.get(field_name) for field_name in self.ordered_field_names}
# then add others whose not defined in order list
for key, value in self.fields.items():
if key not in new_fields:
new_fields[key] = value
self.fields = new_fields
Related
I am building a notification system for a company, where admin users can create Projects and add users to them. The Project model has 9 attributes but I only want to show 3 or 4 fields when a Project is created, but show them all when an existing Project is updated.
This change will only need to be reflected on the Django admin site, so I have extended the ProjectAdmin with my own ProjectForm, where I extend the init method to check if it is a new instance and if so remove certain fields.
# models.py
class Project(models.Model):
project_number = models.IntegerField()
name = models.CharField(max_length=100)
permit = models.CharField(max_length=100, blank=True, default='')
is_active = models.BooleanField(default=True)
users = models.ManyToManyField(CustomUser, blank=True, related_name='project_users')
# add a default
levels = models.ManyToManyField('Level', blank=True, related_name='project_levels')
total_contract_hours = models.IntegerField(default=0, blank=True, verbose_name='Total Design Hours')
hours_used = models.IntegerField(default=0, blank=True, verbose_name='Total Design Hours Used')
notes = models.ManyToManyField('notes.ProjectNote', related_name='core_project_notes', blank=True)
history = HistoricalRecords()
def __str__(self):
ret_str = "{} {}".format(self.project_number, self.name)
if self.permit:
ret_str += " | Permit: {}".format(self.permit)
return ret_str
# admin.py
class ProjectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
attrs = {'class': 'form-control', 'required': True}
if self.instance and self.instance.pk is None:
# creating project
exclude = ['is_active', 'users', 'levels', 'hours_used', 'notes']
for field in exclude:
try:
del self.fields[field]
except ValueError:
print('{} does not exist'.format(field))
for field in self.fields.values():
field.widget.attrs = attrs
class Meta:
model = Project
fields = ['project_number', 'name', 'total_contract_hours']
class ProjectAdmin(admin.ModelAdmin):
form = ProjectForm
fields = ['project_number', 'name', 'permit', 'is_active', 'users', 'levels', 'total_contract_hours', 'hours_used', 'notes']
As I stated I only want basic Project fields on creation, but show all attributed when updating existing Project. With just these changes, I now get a KeyError:
KeyError: "Key 'is_active' not found in 'ProjectForm'. Choices are:
name, permit, project_number, total_contract_hours."
However, when I print the available fields it returns an OrderedDict with all of the model attributes as keys. What am I doing wrong? Thanks!
I figured it out, the field must be in listed in Meta and then you just set the field to be a hidden field.
class ProjectForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
print("Adding project")
if not self.instance or self.instance.pk is None:
for name, field in self.fields.items():
if name in ['design_manager', ]:
field.widget = forms.HiddenInput()
class Meta:
model = Project
fields = ['project_number', 'name', 'design_manager', 'total_contract_hours']
class ProjectAdmin(admin.ModelAdmin):
form = ProjectForm
def save_model(self, request, obj, form, change):
obj.design_manager = request.user
super().save_model(request, obj, form, change)
I have a model like this:
class News(models.Model):
is_activity = models.BooleanField(default=False)
activity_name = models.CharField(max_length=240, blank=True, null=True)
What I am trying to achieve is, if is_activity is checked in I want activity_name to be required. Thus, I am trying to override the __init__ method:
class NewsForm(forms.ModelForm):
class Meta:
model = News
def __init__(self, *args, **kwargs):
super(NewsForm, self).__init__(*args, **kwargs)
if self.fields['is_activity'] is True:
self.fields['activity_name'].required = True
class NewsAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
form = NewsForm
Even if I check in the is_activity the activity_name is non-required. What's wrong?
The ModelForm.clean() method gives you access to the cleaned data – this is where you can include the field-specific conditional logic:
from django.core.validators import EMPTY_VALUES
class NewsForm(forms.ModelForm):
class Meta:
model = News
def clean(self):
is_activity = self.cleaned_data.get('is_activity', False)
if is_activity:
# validate the activity name
activity_name = self.cleaned_data.get('activity_name', None)
if activity_name in EMPTY_VALUES:
self._errors['activity_name'] = self.error_class([
'Activity message required here'])
return self.cleaned_data
class NewsAdmin(FrontendEditableAdminMixin, admin.ModelAdmin):
form = NewsForm
I have a models.py:
class MyModel(models.Model):
...
fries_with_that = models.BooleanField()
forms.py:
class MyModelForm(ModelForm):
class Meta:
model = MyModel
fields = (
'fries_with_that',
)
This works fine, and gives me drop-down with 'unknown', 'yes' and 'no' as choices. But I really want a checkbox. So I added:
Edit
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['fries_with_that'].widget = forms.CheckboxInput()
Now I get a checkbox but when checked, the form returns a value of None. Why is this?
Edit
views.py:
form = MyModelForm(request.POST or None)
if form.is_valid():
# UPDATE PROJECT
updated = MyModel.objects.filter(
project_id=project_id
).update(**form.cleaned_data)
if updated == 0:
project = form.save()
Try this:
class MyModelForm(ModelForm):
fries_with_that = forms.BooleanField(widget=forms.CheckboxInput, default=False)
class Meta:
model = MyModel
fields = (
'fries_with_that',
)
actually, this should render the checkbox.
fries_with_that = forms.BooleanField()
I want to be able to add fields to django admin form at runtime. My model and form:
#admin.py
class SitesForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(SitesForm, self).__init__(*args, **kwargs)
self.fields['mynewfield'] = forms.CharField()
class SitesAdmin(admin.ModelAdmin):
form = SitesForm
admin.site.register(Sites,SitesAdmin)
#model.py
class Sites(models.Model):
url = models.URLField(u'URL')
is_active = models.BooleanField(default=True, blank=True)
is_new = models.BooleanField(default=False, blank=True)
group = models.ForeignKey('SitesGroup')
config = models.TextField(blank=True)
Field mynewfield isn't displayed in form. Why?
You shouldn't be adding a new field to your form in that way, you can just do it as you would any other field and the form will contain both the Model's original fields and your new fields:
class SitesForm(forms.ModelForm):
mynewfield = forms.CharField(max_length=255, blank=True)
class Meta:
model = Sites
class SitesAdmin(admin.ModelAdmin):
form = SitesForm
admin.site.register(Sites, SitesAdmin)
Edit: Sorry, should have read what you had written a little better. If you want a dynamic field like that, then you need to do the following and it will do exactly what you want:
class SitesForm(forms.ModelForm):
class Meta:
model = Sites
def __init__(self, *args, **kwargs):
self.base_fields['mynewfield'] = forms.CharField(max_length=255, blank=True)
super(SitesForm, self).__init__(*args, **kwargs)
class SitesAdmin(admin.ModelAdmin):
form = SitesForm
admin.site.register(Sites, SitesAdmin)
It's the base_fields that gets composed by the metaclass that holds the fields that the form will use.
Solution:
class AdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(AdminForm, self).__init__(*args, **kwargs)
self.fields.insert(1, 'myfield', forms.CharField())
class MyAdmin(admin.ModelAdmin):
form = AdminForm
def get_fieldsets(self, request, obj=None):
return (
(None, {
'fields': (..., 'myfield',),
}),
)
I need to reorder the fields in a modelform that came from another base class. Couldn't find a solution to that. "Address" in the snippet below always show up at the beginning in the HTML template. How can I move it further down with the rendered template? Thanks in advance.
class Address:
street= ...
city= ...
class Customer(Address):
name = ...
...
class CustomerForm(ModelForm):
def __init__(...)
super(CustomerForm, self).__init__(*args, **kw)
self.fields.keyOrder=[
'name',
'Address', #<-- I want "Address" appear after the name in my template
#<-- This obviously is not the right code.
class Meta:
model = Customer
-P
In Django 1.9 they add new argument to Form class
Now you can change the order by defining field_order
for example adding two fields to userena application form:
class SignupFormExtra(SignupForm):
"""
A form to demonstrate how to add extra fields to the signup form, in this
case adding the first and last name.
"""
first_name = forms.CharField(label=_(u'First name'),
max_length=30,
required=False)
last_name = forms.CharField(label=_(u'Last name'),
max_length=30,
required=False)
field_order=['first_name','last_name']
You can use it to any form that inherit class Form.
By default Form.field_order=None, which retains the order in which you define the fields in your form class. If field_order is a list of field names, the fields are ordered as specified by the list and remaining fields are appended according to the default order.
Re-ordering is quite tedious:
# django's SortedDict stores ordering of its fields in this list-type attribute:
keyorder = self.fields.keyOrder
# Remove fields which we want to displace:
keyorder.remove('street')
keyorder.remove('city')
# Get position where we want them to put in:
i = keyorder.index('name') + 1
# Insert items back into the list:
keyorder[i:i] = ['city', 'street']
probably better just to list all the fields again in proper order:
class MyForm(ModelForm):
class Meta:
model=Customer
fields=[..., 'name', 'street', 'city', ...]
from django import forms
class CustomForm(forms.Form):
ORDER = ('field1', 'field2', 'field3')
def __init__(self, *args, **kwargs):
super(CustomForm, self).__init__(*args, **kwargs)
fields = OrderedDict()
for key in self.ORDER:
fields[key] = self.fields.pop(key)
self.fields = fields
When we upgraded to Django 1.7 Skyjur's use of .keyOrder stopped working (Django is using collections.OrderedDict now instead). As a result, I had to find a work-around and this is what appears to be working for me:
from collections import OrderedDict
...
class MyForm(forms
def __init__(self, *args, **kwargs):
fields = OrderedDict()
for key in ("my_preferred_first_field", "my_preferred_second_field"):
fields[key] = self.fields.pop(key)
for key, value in self.fields.items():
fields[key] = value
self.fields = fields
In newer Django the fields attribute is a collections.OrderedDict which since Python 3.2 has a new method move_to_end, so you can simply:
class CustomForm(forms.Form):
...
# Note the following must contain all the fields in the form in the order
# you desire.
CUSTOM_ORDER = ('field1', 'field2', 'field3')
def __init__(self, *args, **kwargs):
super(CustomForm, self).__init__(*args, **kwargs)
# Use custom order for form fields.
for field_name in CUSTOM_ORDER:
self.fields.move_to_end(field_name)