15

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.

2 Answers 2

26

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.)

3
  • 1
    Thanks @daniel, I was stuck with this error from last one week
    – Anuj TBE
    Oct 12, 2017 at 17:11
  • 1
    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.
    – Anuj TBE
    Oct 12, 2017 at 17:30
  • 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. Oct 12, 2017 at 17:37
10

for me it is fixed by adding fields variable like this

model = xxxxxxxxxx fields = '__all__'

after model name

refer to this url

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.