Is it possible to get all the fields from a given model
without writing list of all the fields in the view.py
file when working with UpdateView
in class-based views?
class UserProfileEditView(UpdateView):
model = UserProfile
fields = ['first_name', 'last_name', 'email', 'level', 'course', 'country', 'title', 'avatar', 'icon', 'instagram']
slug_field = 'username'
template_name = 'profile.html'
context_object_name = 'User'
What I mean is instead of writing all the fields in a list (e.g 'first_name', 'last_name'... etc.), can I use something to get all of the fields from given model.
The fields
attribute on a generic class-based view works the same as it does on a ModelForm
. You can explicitly list the fields yourself, or you can use __all__
, which indicates that all fields from the model should be used.
class UserProfileEditView(UpdateView):
model = UserProfile
fields = '__all__'
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With