I am new to Django and Python and wanted to ask you how I can get models and forms in one class. I want to use Radio Buttons, EmailField, DateTimeField so I need really both (models and forms) :
class Post(models.Model):
BOOL_CHOICES = ((True, 'male'), (False, 'female'))
name= models.CharField(max_length=255)
gender= models.BooleanField(choices=BOOL_CHOICES)
Of course I can not write class Post(models.Model) if I use models and forms but I dont know what to do to dispaly both in my view.
Thanks in advance
I don't know why you think you need to do this in the model. If you want to customize the form representation of a model, then you simply define a custom modelform and override the relevant field:
class PostForm(forms.ModelForm):
gender = forms.TypedChoiceField(choices=BOOL_CHOICES, widget=RadioSelect,
coerce=bool)
class Meta:
model = Post
Related
I am trying to understand the process of generating generic form views in django. I have a generic view class with just
class BookUpdate(UpdateView):
model = Book
fields = [ 'name',
'pages',
'categorys'
]
which automatically generates a working html form from my model data. But now, I want to modify the field that is shown for categorys, is there any way to do this, or do I have to create a complete working BookForm class and custom BookUpdate class? Here its just 3 fields, but in my real case there are maybe 15 fields that I would need to code by myself, just because of a tiny change in the category field.
Cant I just overwrite the single field, using any class method?
You can either specify fields or form_class in your generic class-based view. With fields, Django will use a modelform_factory to generate the form. There's not much you can customise then.
You should create a BookForm class so that you can customise the fields. In your BookUpdate view, you only need to remove fields and add form_class = BookForm. Here I'm customising the widget for categorys and overriding the form field for pages:
def BookUpdate(UpdateView):
model = Book
form_class = BookForm
def BookForm(ModelForm):
pages = MyCustomPagesField()
class Meta:
model = Book
fields = '__all__'
widgets = {'categorys': MyCustomWidget()}
Note that you don't have to specify all fields, you can use "__all__" to have all fields or you can set exclude = [<list fields to exclude>] to just exclude a couple.
You don't have to code the fields yourself. But there is a small amount of work to do, as there isn't a method to override.
What you need to do is define a custom form. Since that will be a ModelForm, it will use the same logic to automatically create its fields based on the model. You can then override the definition of one of them.
class BookForm(forms.ModelForm):
categorys = forms.ModelMultipleChoiceField(custom_attributes_here...)
class Meta:
model = Book
fields = ["name", "pages", "categorys"]
And now tell your view to use that form:
class BookUpdate(UpdateView):
form_class = BookForm
For example, lets say this is my model:
class Author(models.Model):
author = models.CharField()
friends = models.CharField()
and I only want to have the friends as a form field
Are you using a ModelForm? If so, you set the fields you want using the fields variable in the Meta class:
from django import forms
from yourapp.models import Author
class AuthorForm(forms.ModelForm):
class Meta:
model = Author
fields = [ "friends", ]
Note: The fields variable is a list, so make sure you include the trailing comma if you're only passing a single field.
Suggest that you have a read of the Django ModelForm Docs as well, as that way you'll get a bit ahead on all the other questions you're probably about to come up with :)
I have two questions concerning models and forms.
1) What is the best way to create automatically forms for the models?
In the example below I have two models - ModelA and ModelB. I need forms for them - ModelAForm and ModelBForm. They should be defined automatically. I do not want to do it manually, because in the future I will add other models, and all the forms will look the same. I am thinking about creating special decorator for models and use modelform_factory.
from django.db import models
from django.forms import ModelForm
class ModelA(models.Model):
...
class ModelB(models.Model):
...
class ModelAForm(ModelForm):
class Meta:
abstract = ModelA
class ModelBForm(ModelForm):
class Meta:
abstract = ModelB
2) Assuming I am using only ModelForm forms, it is possible to find the form for the model? Example. I have two models ModelA and ModelB, and two forms ModelAForm and ModelBForm. I have instance of ModelA and I would like to identify proper form for this model which I will pass to template - in this case ModelAForm.
Django provides some generic editing views. You only have to provide a model and the view will generate the form automatically. If you want to use the forms to create or update instances you can just use them.
If your really need to create the forms yourself you can use the modelform_factory that these views use themself to create the correct form for your model. But i would first go with the generic views as long as they can be modified to suit your needs.
Just pass it your model and optionally the fields you want. If you omit the fields, all fields will be generated:
from .models import MyModel
from django.forms.models import modelform_factory
my_form = modelform_factory(MyModel, fields=['name', 'age', 'job'])
There's ModelForm in django.
Here's fragment of documentation and link for it.
https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter']
You're probably looking for generic class-based views, where you only pass form_class and model instances, everything else django handles without your help.
Link for them - https://docs.djangoproject.com/en/1.10/ref/class-based-views/
Is there any way to use a Form Wizard in Admin interface for add/edit Models.
(using Django 1.5.2)
for example:
--models.py--
class AModel(models.Model):
fieldA = models.CharField(max_length=64)
fieldB = models.CharField(max_length=64)
--admin.py--
class Form1(ModelForm):
class Meta:
model = AModel
fields = ('fieldA',)
class Form2( ModelForm ):
class Meta:
model = AModel
fields = ('fieldB',)
.... something add for make this two forms in one multipage admin form , is that possible? or any other way to do the same job.
Thanks in advance.
It isn't possible by using admin module. The only way is either some external plugins or you need to create custom admin views. Google around..
Given:
#models.py
from django.db import models
class MyModel(models.Model):
myfield = models.IntegerField('Detailed description')
#forms.py
from django import forms
from models import MyModel
class MyModelForm(forms.ModelForm):
myfield = forms.IntegerField()
class Meta:
model = MyModel
With this setup - verbose_name will not be shown on admin page, name of field will be instead (with capitalized first letter). If remove overriding field from MyModelForm - verbose name will be shown as usual. If provide string with description as argument label, forms.IntegerField() - it will be displayed on admin page.
Im looking for pythonic way to display verbose_name defined in model class in case if this field form is overrided in ModelForm, without using label hint.
Any thoughts?
You can access the verbose name of the model in the form like this:
myfield = models.IntegerField(label=MyModel._meta.get_field_by_name('myfield')[0].verbose_name)