Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all fields in UpdateView django

Tags:

field

django

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.

like image 917
Shosalim Baxtiyorov Avatar asked Nov 16 '18 03:11

Shosalim Baxtiyorov


1 Answers

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__'
    ...
like image 97
Will Keeling Avatar answered Nov 15 '22 08:11

Will Keeling