Django AttributeError 'ModelFormOptions' object has no attribute 'concrete_fields'

11.5k Views Asked by At

I get an AttributeError saying 'ModelFormOptions' object has no attribute 'concrete_fields'. My code is as follow:

My model is like this:

class OrderReceiving(models.Model):
    user = models.ForeignKey(User)
    po = models.ForeignKey(Order)
    datetime = models.DateTimeField(default=timezone.now)
    product = models.ForeignKey(Product)
    quantity = models.IntegerField(default=0)
    building = models.ForeignKey(BuildingNumber)
    isele_abc = models.ForeignKey(IseleAbc)
    isele_num = models.ForeignKey(IseleNum)
    isele_floor = models.ForeignKey(IseleFloor)

My ModelForm is like this:

class OrderReceivingForm(ModelForm):

    class Meta:
        model = OrderReceiving
        exclude = ['user', 'datetime']
        labels = {
            'po': _('Vendor PO ID'),
        }

and my view is:

class OrderReceivingView(View):
    template = 'poreceiving/po_receive.html'

    def get(self, request, *args, **kwargs):
        context = {}
        form = OrderReceivingForm()
        context['form'] = form
        return render_to_response(self.template,
                              RequestContext(request, context))

    def post(self, request, *args, **kwargs):
        receive_form = OrderReceivingForm(request.POST)
        if receive_form.is_valid():
            receive_form.save()
        else:
            context = {}
            form = OrderReceivingForm(instance=receive_form)
            context['form'] = form
            return render_to_response(self.template,
                                  RequestContext(request, context))
        return HttpResponse('Hi everyone')

Here when I post invalid form then I get

AttributeError   'ModelFormOptions' object has no attribute 'concrete_fields'.

I expect to get form with error message. Please help me out.

My Traceback:

Traceback:
File "/home/student/virtualenvs/alex/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/student/virtualenvs/alex/lib/python2.7/site-packages/django/views/generic/base.py" in view
71.             return self.dispatch(request, *args, **kwargs)
File "/home/student/virtualenvs/alex/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
89.         return handler(request, *args, **kwargs)
File "/home/student/git/ezoffer-pos2/poreceiving/views.py" in post
42.             form = OrderReceivingForm(instance=receive_form)
File "/home/student/virtualenvs/alex/lib/python2.7/site-packages/django/forms/models.py" in __init__
320.             object_data = model_to_dict(instance, opts.fields,    opts.exclude)
File "/home/student/virtualenvs/alex/lib/python2.7/site-packages/django/forms/models.py" in model_to_dict
132.     for f in chain(opts.concrete_fields, opts.virtual_fields, opts.many_to_many):

Exception Type: AttributeError at /po/receiving/
Exception Value: 'ModelFormOptions' object has no attribute   'concrete_fields'
2

There are 2 best solutions below

0
On BEST ANSWER

This line:

form = OrderReceivingForm(instance=receive_form)

makes no sense. You can't pass a form as an instance to a form. The instance parameter refers to an instance of the model, which you pass when you want to update an existing model instance (rather than creating a new one). If you're not updating an existing OrderReceiving, don't use instance at all.

0
On

Your problem is on this line

form = OrderReceivingForm(instance=receive_form)

You are passing a form instance instead of a model instance (OrderReceiving), which is what OrderReceivingForm is expecting. You should do something like this instead

order = OrderReceiving.objects.get(...)
form = OrderReceivingForm(instance=order)

or remove the instance parameter altogether and just have this

form = OrderReceivingForm()