After switching to inline formset I ended up with:
def dns_view(request, domain):
dnszone = get_object_or_404(DNSSQL, zone = domain)
form1 = EditDNSZone(instance = dnszone)
forms = EditDNSEntry(instance = dnszone, prefix = 'entries')
formsmx = EditDNSEntryMX(instance = dnszone, prefix = 'mxentries')
After trying to save all forms I managed to save only form1.
How do I save all forms?
Django's formset is for multiple instances of the same form. You are trying to save multiple form classes, which is not what formset for.
One way is to build a form contains all the fields in the form your want to include, and when processing the form, create each individual form you want to process. The following is an simple illustration. You do something fancy too, by introspecting the models and create model forms automatically, but that's a long story...
class Form1(forms.Form):
a_field = forms.CharField()
class Form2(forms.Form):
b_field = forms.CharField()
class MainForm(forms.Form):
a_field = forms.CharField()
b_field = forms.CharField()
def __init__(self, **kwargs):
super(MainForm, self).__init__(**kwargs)
# This will work because the field name matches that of the small forms, data unknow to
# a form will just be ignored. If you have something more complex, you need to append
# prefix, and converting the field name here.
form1 = Form1(**kwargs)
form2 = Form2(**kwargs)
Related
I am working on a project which is administered by a super admin who puts in data for different companies.
Lets say, I have these models:
class Company(models.Model):
name = models.CharField(max_length=100)
class ContactPerson(models.Model):
name = models.CharField(max_length=100)
company = models.ForeignKey(Company)
class Item(models.Model):
company = models.ForeignKey(Company)
contact_person = models.ForeignKey(ContactPerson)
I need to ensure that I (in django admin) in the edit mode I only see contact persons which belong to the selected company.
Being not in the year 2005 anymore I want to avoid writing loads of super ugly jQuery code.
I guess I could overwrite the admin form for Item. But still I had to make the contact_person optional, so when I create a new Item, the list of contact persons need to be empty. Then I'd select a company, save it and go back to edit. Now the contact_person list would be filled and I could add somebody. But if I now change the comany, I'd have to remove all selected contact persons. Sure, I could to this in the form... but it looks SO hacky and not like a nice django solution.
Anybody got some fancy ideas?
Actually, django provided me with a neat solution.
When you look at the UserAdmin class within the django code, you'll find a built-in way to handle a two-step creation process.
#admin.register(User)
class UserAdmin(admin.ModelAdmin):
...
add_form = UserCreationForm
...
def get_form(self, request, obj=None, **kwargs):
"""
Use special form during user creation
"""
defaults = {}
if obj is None:
defaults['form'] = self.add_form
defaults.update(kwargs)
return super().get_form(request, obj, **defaults)
When the attribute add_form is set and the object has no id yet (= we are creating it), it takes a different form than usual.
I wrapped this idea in an admin mixin like this:
class AdminCreateFormMixin:
"""
Mixin to easily use a different form for the create case (in comparison to "edit") in the django admin
Logic copied from `django.contrib.auth.admin.UserAdmin`
"""
add_form = None
def get_form(self, request, obj=None, **kwargs):
defaults = {}
if obj is None:
defaults['form'] = self.add_form
defaults.update(kwargs)
return super().get_form(request, obj, **defaults)
Now, when I have dependent fields, I create a small form, containing all values independent of - in my case - company and a regular form containing everything.
#admin.register(Item)
class ItemAdmin(AdminCreateFormMixin, admin.ModelAdmin):
form = ItemEditForm
add_form = ItemAddForm
...
Now I can customise the querysets of the dependent field in my edit form:
class ItemEditForm(forms.ModelForm):
class Meta:
model = Item
exclude = ()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['contact_person'].queryset = ContactPerson.objects.filter(company=self.instance.company)
The only drawback is, that all dependent fields need to be nullable for the database. Otherwise you wouldn't be able to save it in the creation process.
Luckily, you can tell django that a field is required in the form but not on database level with blank=False, null=True in the model declaration.
Hope this helps somebody else as well!
Using Django 1.11, one of my models is an array stored within a django-jsonfield field.
class MyModel(models.Model)
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=100)
core = JSONField(blank=True, null=True, default=None)
I am using a ModelForm in a couple of views to create and edit new instances. Within the ModelForm I'm borrowing the django.contrib.postgres.forms.SimpleArrayField to parse the input into the field.
Adding a new model is fine, but in the edit version, the array gets pre-populated with what looks like the __str__ representation (eg an array of 1,2,3 becomes ['1','2','3'].
I'm getting around this by parsing the array into initial= for each form but I'd rather do this in one place (DRY) rather than having to repeat it inside each view and form instance.
Are there any hooks or methods (perhaps a custom widget?) that means I can do this just once in the form or somewhere else?
Snippet of the current view with hacky approach using initial=:
def edit_mymodel(id):
current_instance = MyModel.objects.get(pk=id)
if request.method == "GET":
form = MyModelForm(instance=current_instance,
initial={"core": ",".join(current_instance.core)}
)
return render(request, 'network_manager/edit.html',
{'form': form}
)
You can override __init__
class MyModelForm(ModelForm)
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.initial['core'] = ",".join(self.instance.core)
There is a problem, I need to submit two interrelated modelforms with one html form. I know how to submit two separate forms, but foreign key makes me crazy in case of related modelforms.
The problem is, that second form should have filled field with foreign key to instance from first form.
In this particular case I decided to merge two models, but I think, there should be cases, where workaround for described problem would be useful.
Please consider following code:
Models:
from django.db import models
class Facility(models.Model):
name = models.CharField(max_length=255)
class FacilityDetail(models.Model):
some_details = models.CharField(max_length=255)
facility = models.ForeignKey(Facility)
Corresponding django forms:
from django import forms
class FacilityForm(forms.ModelForm):
class Meta:
model = Facility
fields = ('name')
class FacilityDetailForm(forms.ModelForm):
class Meta:
model = FacilityDetail
fields = ('some_details', 'facility')
View to handle forms:
from django.views.generic import View
FACILITY_PREFIX = 'facility'
FACILITY_DETAIL_PREFIX = 'facility_detail'
class FacilityCreateView(View):
def get(self, request, *args, **kwargs):
facility_form = FacilityForm(prefix=FACILITY_PREFIX)
facility_detail_form = FacilityDetailForm(prefix=FACILITY_DETAIL_PREFIX)
context = {
'facility_form': facility_form,
'facility_detail_form': facility_detail_form,
}
return render(request, 'facility_create.html', context)
def post(self, request, *args, **kwargs):
facility_form = FacilityForm(request.POST, prefix=FACILITY_PREFIX)
facility_detail_form = FacilityDetailForm(request.POST, prefix=FACILITY_DETAIL_PREFIX)
if facility_form.is_valid():
facility = facility_form.save()
# is not valid, because there is no `facility`
if facility_detail_form.is_valid():
facility_detail_form.cleaned_data['facility'] = facility
facility_detail_form.save()
return redirect(...)
context = {
'facility_form': facility_form,
'facility_detail_form': facility_detail_form,
}
return render(response, 'facility_list.html', context)
How should I handle form validation and saving in FacilityCreateView.post?
One way you could fix this is:
facility_detail_form.cleaned_data['facility'] = facility
could be replaced with:
facility_detail = facility_detail_form.save(commit=False)
facility_detail.facility = facility
facility_detail.save()
#rest of the code.. .
Here commit=False creates the object for you, without saving it to the database, where you can assign your foreign key object before saving.
I was wondering how to auto populate fields in a form like in this picture http://prntscr.com/lkn7x . Here is what I have so far for my forms.
class PIREPForm(ModelForm):
class Meta:
model = PIREP
In the model form you can pass the instance which will be mapped to the form fields
form = TestForm(instance = test_instance)
Otherwise, if you want to populate some fields you could pass the initial argument
form = TestForm(initial = {'field_name':field_value,...})
A Django autofield when displayed using a formset is hidden by default. What would be the best way to show it?
At the moment, the model is declared as,
class MyModel:
locid = models.AutoField(primary_key=True)
...
When this is rendered using Django formsets,
class MyModelForm(ModelForm):
class Meta:
model = MyModel
fields = ('locid', 'name')
it shows up on the page as,
<input id="id_form-0-locid" type="hidden" value="707" name="form-0-locid"/>
Thanks.
Edit
I create the formset like this -
LocFormSet = modelformset_factory(MyModel)
pformset = LocFormSet(request.POST, request.FILES, queryset=MyModel.objects.order_by('name'))
Second Edit
Looks like I'm not using the custom form class I defined there, so the question needs slight modification..
How would I create a formset from a custom form (which will show a hidden field), as well as use a custom queryset?
At the moment, I can either inherit from a BaseModelFormSet class and use a custom query set, or I can use the ModelForm class to add a custom field to a form. Is there a way to do both with a formset?
Third Edit
I'm now using,
class MyModelForm(ModelForm):
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
locid = forms.IntegerField(min_value = 1, required=True)
self.fields['locid'].widget.attrs["type"] = 'visible'
self.queryset = MyModel.objects.order_by('name')
class Meta:
model = MyModel
fields = ('locid', 'name')
LocFormSet = modelformset_factory(MyModel, form = MyModelForm)
pformset = LocFormSet()
But this still doesn't
Show locid
Use the custom query that was specified.
Try changing the default field type:
from django import forms
class MyModelForm(ModelForm):
locid = forms.IntegerField(min_value=1, required=True)
class Meta:
model = MyModel
fields = ('locid', 'name')
EDIT: Tested and works...
As you say, you are not using the custom form you have defined. This is because you aren't passing it in anywhere, so Django can't know about it.
The solution is simple - just pass the custom form class into modelformset_factory:
LocFormSet = modelformset_factory(MyModel, form=MyModelForm)
Edit in response to update 3:
Firstly, you have the redefinition for locid in the wrong place - it needs to be at the class level, not inside the __init__.
Secondly, putting the queryset inside the form won't do anything at all - forms don't know about querysets. You should go back to what you were doing before, passing it in as a parameter when you instantiate the formset. (Alternatively, you could define a custom formset, but that seems like overkill.)
class MyModelForm(ModelForm):
locid = forms.IntegerField(min_value=1, required=True)
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['locid'].widget.attrs["type"] = 'visible'
class Meta:
model = MyModel
fields = ('locid', 'name')
LocFormSet = modelformset_factory(MyModel, form = MyModelForm)
pformset = LocFormSet(request.POST, request.FILES,
queryset=MyModel.objects.order_by('name')))
Okay, none of the approaches above worked for me. I solved this issue from the template side, finally.
There is a ticket filed (http://code.djangoproject.com/ticket/10427), which adds a "value" option to a template variable for a form. For instance, it allows,
{{form.locid.value}}
to be shown. This is available as a patch, which can be installed in the SVN version of django using "patch -p0 file.patch"
Remember, the {{form.locid.value}} variable will be used in conjunction with the invisible form - otherwise, the submit and save operations for the formset will crash.
This is Not the same as {{form.locid.data}} - as is explained in the ticket referred to above.
The reason that the autofield is hidden, is that both BaseModelFormSet and BaseInlineFormSet override that field in add_field. The way to fix it is to create your own formset and override add_field without calling super. Also you don't have to explicitly define the primary key.
you have to pass the formset to modelformset_factory:
LocFormSet = modelformset_factory(MyModel,
formset=VisiblePrimaryKeyFormSet)
This is in the formset class:
from django.forms.models import BaseInlineFormSet, BaseModelFormSet, IntegerField
from django.forms.formsets import BaseFormSet
class VisiblePrimaryKeyFormset(BaseModelFormSet):
def add_fields(self, form, index):
self._pk_field = pk = self.model._meta.pk
if form.is_bound:
pk_value = form.instance.pk
else:
try:
pk_value = self.get_queryset()[index].pk
except IndexError:
pk_value = None
form.fields[self._pk_field.name] = IntegerField( initial=pk_value,
required=True) #or any other field you would like to display the pk in
BaseFormSet.add_fields(self, form, index) # call baseformset which does not modify your primary key field