Django: Replacing form field with another form - django

Is there a simple way to replace a form field in Django with another form? Specifically, I have a one-to-one relationship between two models, and it would be great if I could define a field to actually be defined as another form, something like this:
class FirstModelForm(forms.ModelForm):
class Meta:
model = FirstModel
class SecondModelForm(forms.ModelForm):
second = forms.InnerForm(form=FirstModelForm)
class Meta:
model = SecondModel
Any ideas? Or should I write it myself and submit to Django codebase? ;-)

Related

What are form factories and when should they be used?

Would someone like to attempt a succinct explanation of what a form_factory, or a modelform_factory is in relation to a form / modelform?
Why use a factory?
Is there an official technical definition for a
factory method?
The modelform_factory method is a method that returns a ModelForm class. Here's an example of why it's useful.
If you have a simple view that uses a form for a single model, then there is no need for a factory, you can simply define the form:
class ProductForm(forms.ModelForm):
class Meta:
model = Product
def add_product_view(request):
form = ProductForm()
...
However, if your view needs to handle multiple models, then you might not want to have to define a form class for every model. The modelform_factory lets you create the form class dynamically.
apps.get_model('newapp', 'mymodel')
def add_model_view(request, model_name):
Model = apps.get_model('myapp', model_name)
Form = modelform_factory(Model)
The Django admin uses modelform_factory to generate model form classes dynamically. This means you don't need to define a model form every time you register a model in the Django admin.

Django forms: How to simply include all attributes in the associated model

I am working through how to use Django's forms (https://docs.djangoproject.com/en/1.11/topics/forms/#more-on-fields) and I can't see a way to generate a form structure that is based on a defined Model. In Symfony, I remember I was able to get my form to automatically include all parameters of myModel (for example) even if any new attributes were later added to the model.
For example:
class myModel(models.Model):
name = models.CharField(max_length=50)
created=models.DateTimeField(null=False)
modified=models.DateTimeField(null=True)
myParameter= models.IntegerField(default=None)
// ... plus many more parameters
Rather than having to manually type corresponding rows into my class myModelForm(forms.Form):, I'm looking/hoping for a 'catch all'.
from django.forms import ModelForm
class myModelForm(ModelForm):
class Meta:
model = myModel
fields = '__all__'
More details selecting-the-fields-to-use

Django Populate Dropdown with Records

I'm only using forms.Form but I'm trying to show two choice fields that have selections of the associated models.
It basically needs to show the same names but in both fields. Here's what I'm using.
class ManagersForm(forms.Form):
class Meta:
model = A
leader = forms.ChoiceField()
co-leader = forms.ChoiceField()
Is there not just a way that I can parse the users?
users = MyUser.objects.filter(a=i)
You need to use a ModelForm not Form, if the field from the Model is a ForeignKey the form will render the field as a dropdown of the associated model:
class ManagersForm(forms.ModelForm):
class Meta:
model = A

Django: model maxlength and form maxlength

I need to somehow hook the Model's max_length constraints into a Form object.
Say I define a Model with a field: name = models.CharField(max_length=30)
Now I define a Form object with the same field: name = forms.CharField(max_length=30)
Question is, is there someway to synchronize the two? If I define a Model first, could I define the max_length of the Form class based on what I did with the Model class?
Using a ModelForm makes sense if you have a form related directly to a model.
Another way to pick up the max_length attribute from a model is to use the _meta attribute of the model like so:
>>> SomeModel._meta.get_field('some_field').max_length
64
>>>
so:
from models import *
class MyForm(forms.Form):
some_field = forms.CharField(label='Some Field',
max_length=SomeModel._meta.get_field('some_field').max_length)
CharField docs
Use ModelForms:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform
This way the forms inherit directly from the models and you do not have to repeat yourself.

Admin Form Integration for Custom Model Fields in Django

I need a model field composed of a numeric string for a Django app I'm working on and since one doesn't exist I need to roll my own. Now I understand how "get_db_prep_value" and such work, and how to extend the Model itself (the django documentation on custom model fields is an invaluable resource.), but for the life of me I can't seem to figure out how to make the admin interface error properly based on input constraints.
How do I make the associated form field in the admin error on incorrect input?
Have a look at the Form and field validation section in the Django documentation, maybe that's what you're looking for?
You would have to make a new type of form field for your custom model field.
All you need to do is define a custom modelform which uses your new field, and then tell the admin to use that form to edit your models.
class MyModelForm(forms.ModelForm):
myfield = MyCustomField()
class Meta:
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
form = MyModelForm