Xus
Xus

Reputation: 198

Select all fields except one Django

There is a class-based view and I want to know how I can use all fields except the user field in the fields instead of '__all__' and also I don't want to write all the fields in a list because there are too many fields and I want to exclude one item.

here is the code:

class TaskCreate(LoginRequiredMixin, CreateView):
    model = Task
    fields = '__all__' # ! here
    success_url = reverse_lazy('Tasks')

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(TaskCreate, self).form_valid(form)

many thanks in advance.

Upvotes: 1

Views: 2947

Answers (1)

Bivek Chalise
Bivek Chalise

Reputation: 86

I think you need to define a ModelForm and assign it as form_class attribute to TaskCreate then exclude the field in that ModelForm.

For example: Here is an exclude example for Update view

Upvotes: 4

Related Questions