Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Set default widget in model definition

Tags:

django

widget

Is it possible to set a custom widget for a model in the model definition?

For example, having a model ModelA referencing to the model ModelB, I would like that a modelForm of ModelA renderes the reference field to ModelB automatically with a custom widget defined by ModelB.

The use case behind is the creation of a reusable app, where the reusable app should provide the form field widget for developers using the app.

like image 484
Thomas Kremmel Avatar asked Sep 11 '12 12:09

Thomas Kremmel


People also ask

What is default in Django model?

default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. null: If True , Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string).

How can create primary key in Django model?

If you'd like to specify a custom primary key, specify primary_key=True on one of your fields. If Django sees you've explicitly set Field.primary_key , it won't add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).

What is forms ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.

What is Attrs Django?

attrs . A dictionary containing HTML attributes to be set on the rendered DateInput and TimeInput widgets, respectively. If these attributes aren't set, Widget. attrs is used instead.


1 Answers

Is it possible to set a custom widget for a model in the model definition?

Yes, if you override the field: Specifying the form field for a model field.

like image 62
jpic Avatar answered Oct 07 '22 17:10

jpic