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.
Related
SOLUTION AT THE BOTTOM
Problem: Django form populating with list of objects rather than values
Summary: I have 2 models Entities and Breaks. Breaks has a FK relationship to the entity_id (not the PK) on the Entities model.
I want to generate an empty form for all the fields of Breaks. Generating a basic form populates all the empty fields, but for the FK it generates a dropdown list of all objects of the Entities table. This is not helpful so I have excluded this in the ModelForm below and tried to replace with a list of all the entity_ids of the Entities table. This form renders as expected.
class BreakForm(ModelForm):
class Meta:
model = Breaks
#fields = '__all__'
exclude = ('entity',)
def __init__(self, *args, **kwargs):
super(BreakForm, self).__init__(*args, **kwargs)
self.fields['entity_id'] = ModelChoiceField(queryset=Entities.objects.all().values_list('entity_id', flat=True))
The below FormView is the cbv called by the URL. As the below stands if I populate the form, and for the FK column entity_id choose one of the values, the form will not submit. By that field on the form template the following message appears Select a valid choice. That choice is not one of the available choices.
class ContactFormView(FormView):
template_name = "breaks/test/breaks_form.html"
form_class = BreakForm
My initial thoughts were either that the datatype of this field (string/integer) was wrong or that Django needed the PK of the row in the Entities table (for whatever reason).
So I added a post function to the FormView and could see that the request.body was populating correctly. However I can't work out how to populate this into the ModelForm and save to the database, or overcome the issue mentioned above.
Addendum:
Models added below:
class Entity(models.Model):
pk_securities = models.AutoField(primary_key=True)
entity_id = models.CharField(unique=True)
entity_description = models.CharField(blank=True, null=True)
class Meta:
managed = False
db_table = 'entities'
class Breaks(models.Model):
pk_break = models.AutoField(primary_key=True)
date = models.DateField(blank=True, null=True)
entity = models.ForeignKey(Entity, on_delete= models.CASCADE, to_field='entity_id')
commentary = models.CharField(blank=True, null=True)
active = models.BooleanField()
def get_absolute_url(self):
return reverse(
"item-update", args=[str(self.pk_break)]
)
def __str__(self):
return f"{self.pk_break}"
class Meta:
managed = False
db_table = 'breaks'
SOLUTION
Firstly I got this working by adding the following to the Entity Model class. However I didn't like this as it would have consequences elsewhere.
def __str__(self):
return f"{self.entity_id}"
I found this SO thread on the topic. The accepted answer is fantastic and the comments to it are helpful.
The solution is to subclass ModelChoiceField and override the label_from_instance
class EntityChoiceField(ModelChoiceField):
def label_from_instance(self, obj):
return obj.entity_id
I think your problem is two fold, first is not rendering the dropdown correctly and second is form is not saving. For first problem, you do not need to do any changes in ModelChoiceField queryset, instead, add to_field_name:
class BreakForm(ModelForm):
class Meta:
model = Breaks
#fields = '__all__'
def __init__(self, *args, **kwargs):
super(BreakForm, self).__init__(*args, **kwargs)
self.fields['entity_id'] = ModelChoiceField(queryset=Entities.objects.all(), to_field_name='entity_id')
Secondly, if you want to save the form, instead of FormView, use CreateView:
class ContactFormView(CreateView):
template_name = "breaks/test/breaks_form.html"
form_class = BreakForm
model = Breaks
In Django, the request object passed as parameter to your view has an attribute called "method" where the type of the request is set, and all data passed via POST can be accessed via the request. POST dictionary. The view will display the result of the login form posted through the loggedin. html.
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!
I would like to create a mutli-step form in Django that only submits the data for processing at the end of all the steps. Each step needs to be able to access and display data that we entered in previous step(s).
Is there a way to do this with Django? Django's Form-Wizard can't handle this basic functionality.
Of course there's a way to do this in Django.
One way is to hold your values in session until you submit them at the end. You can populate your forms using values held in session if you return to previous step.
With some searching, you may find an app that someone has already written that will do what you want, but doing what you need isn't hard to do with Django, or any other framework.
Example, ignoring import statements:
#models/forms
class Person(models.Model):
fn = models.CharField(max_length=40)
class Pet(models.Model):
owner = models.ForeignKey(Person)
name = models.CharField(max_length=40)
class PersonForm(forms.ModelForm):
class Meta:
model = Person
class PetForm(forms.ModelForm):
class Meta:
model = Pet
exclude = ('owner',)
#views
def step1(request):
initial={'fn': request.session.get('fn', None)}
form = PersonForm(request.POST or None, initial=initial)
if request.method == 'POST':
if form.is_valid():
request.session['fn'] = form.cleaned_data['fn']
return HttpResponseRedirect(reverse('step2'))
return render(request, 'step1.html', {'form': form})
def step2(request):
form = PetForm(request.POST or None)
if request.method == 'POST':
if form.is_valid():
pet = form.save(commit=False)
person = Person.objects.create(fn=request.session['fn'])
pet.owner = person
pet.save()
return HttpResponseRedirect(reverse('finished'))
return render(request, 'step2.html', {'form': form})
We'll assume that step2.html has a link to go back to step1.html.
You'll notice in the step1 view I'm pulling the value for fn from session that was set when the form was saved. You would need to persist the values from all previous steps into the session. At the end of the steps, grab the values, create your objects and redirect to a finished view, whatever that might be.
None of this code has been tested, but it should get you going.
You can easily do this with the form wizard of django-formtools. A simple example would be something like the following.
forms.py
from django import forms
class ContactForm1(forms.Form):
subject = forms.CharField(max_length=100)
sender = forms.EmailField()
class ContactForm2(forms.Form):
message = forms.CharField(widget=forms.Textarea)
views.py
from django.shortcuts import redirect
from formtools.wizard.views import SessionWizardView
class ContactWizard(SessionWizardView):
def done(self, form_list, **kwargs):
do_something_with_the_form_data(form_list)
return redirect('/page-to-redirect-to-when-done/')
urls.py
from django.conf.urls import url
from forms import ContactForm1, ContactForm2
from views import ContactWizard
urlpatterns = [
url(r'^contact/$', ContactWizard.as_view([ContactForm1, ContactForm2])),
]
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)
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)