Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include auto generated primary key in Django ModelForm fields

I have a model where I didn't specify a primary key and Django generated one for me. Now I create a ModelForm for the model and I have specified id in the fields section of ModelForm. However, in my ModelForm object, the id field is not present.

Are only the model fields explicitly declared visible in the ModelForm?

like image 329
shar Avatar asked Feb 01 '26 14:02

shar


1 Answers

Are only the model fields explicitly declared visible in the ModelForm?

Yes, generally you don't want to mess with this field, if the user inputs a value for the id field it's very likely to be duplicated so this is something you want django to take care of for you.

like image 198
e4c5 Avatar answered Feb 03 '26 04:02

e4c5