Using ModelFormMixin without the 'fields' attribute is prohibited
Asked Answered
C

2

16

I'm using Django 1.11

I have created a Form and using Class based view to create a record and save to database.

Business/models.py

class BusinessType(models.Model):
    title = models.CharField(max_length=100)
    created = models.DateTimeField('date created', auto_now_add=True)
    modified = models.DateTimeField('last modified', auto_now=True)

    class Meta:
        db_table = 'business_types'

    def __str__(self):
        return self.title


class Business(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=200)
    business_type = models.ForeignKey(BusinessType, on_delete=models.CASCADE)
    created = models.DateTimeField('date created', auto_now_add=True)
    modified = models.DateTimeField('last modified', auto_now=True)

    class Meta:
        verbose_name = 'business'
        verbose_name_plural = 'businesses'
        db_table = 'businesses'

    def __str__(self):
        return self.name

Business/Forms.py

class BusinessForm(ModelForm):
    class Meta:
        model = Business
        fields = ['user']

Business/views.py

class BusinessCreate(LoginRequiredMixin, CreateView):
    model = Business
    form = BusinessForm

    def form_valid(self, form):
        messages.success(self.request, 'form is valid')
        form.instance.user = self.request.user
        form.save()

    def get_success_url(self):
        messages.success(self.request, 'Business Added Successfully')
        return reverse('business:list')

On loading template of BusinessCreate it gives error as

Using ModelFormMixin (base class of BusinessCreate) without the 'fields' attribute is prohibited.

My Trials

After moving fields to views class, it is working fine. But I don't want to do so, as I may be using this form on multiple views and thus will require changes on multiple pages in future if needed.

Chivalric answered 12/10, 2017 at 4:29 Comment(0)
Z
28

Your form is not being recognised. This is because you have used form to set the attribute in the view, but the correct attribute is form_class.

(Note, if you correctly set form_class, you don't need model as well.)

Zerla answered 12/10, 2017 at 6:12 Comment(3)
Thanks @daniel, I was stuck with this error from last one weekChivalric
can you help a bit more as def get_success_url is not calling after saving record to database. and it is giving error as The view business.views.BusinessCreate didn't return an HttpResponse object. It returned None instead.Chivalric
get_success_url is called by the default implementation of form_valid, which you have overridden. You need to return the redirect from that method, for example return redirect(self.get_success_url()). Note that the messages.success call shouldn't really be in that method.Zerla
T
13

for me it is fixed by adding fields variable like this

model = xxxxxxxxxx fields = '__all__'

after model name

refer to this url

Three answered 21/9, 2019 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.