Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Model Field Metadata

I have a Django model and I wish to denote that some of the model fields are private so that when I display a ModelForm based on this model I can display these fields marked as such.

I'd like this to be specified on the model rather than the form as I think that's where it belongs.

I'm wondering what the best way to do this is. Can I write a decorator @private to do this? Can anyone point me at an example?

Thanks

like image 447
Ludo Avatar asked May 30 '26 03:05

Ludo


1 Answers

There's no one feature that perfectly fits your problem, but here's a couple of suggestions:

  • Add the information to the model's Meta class, which you can then access from your form via the _meta attribute on the model
  • Utilise the help_text option on your model fields (obviously this won't help you to "decide programatically which fields I should be displaying to other users")

I do agree that it's debatable as to whether this belongs at the model layer; it seems like business logic to me.

like image 61
Greg Avatar answered Jun 02 '26 20:06

Greg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!