Escher
Escher

Reputation: 5766

Django model namespace error: missing field in ModelField form

I'm learning django (1.9.2) and have an namespace error in one of my forms that I can't understand when I runserver (also occurs when I makemigrations, just in case my database schema isn't current):

  File "/path/to/my_project/forms.py", line 11, in Meta
    product_code,
NameError: name 'product_code' is not defined

Here is the relevant code:

models.py

from django.db import models
import uuid

class Product(models.Model):
    product_code = models.CharField(max_length=32)
    #other fields, etc

forms.py

from django import forms
from my_project.models import Product

class InsertProduct(forms.ModelForm):
    class Meta:
        model=Product
        fields = (
            product_code,
            #other fields, etc
        )

I'm importing the Product model, (and if I don't, I get an error so it's clearly required) but it doesn't seem to be recognising the Product namespace. If I comment out product_code, the interpreter simply complains that the next field is missing from Product. What must I do here to get my form to work?

Upvotes: 0

Views: 230

Answers (1)

Selcuk
Selcuk

Reputation: 59174

Field names should be strings, therefore you need to enclose them in quotes, such as:

fields = (
    'product_code',
    #other fields, etc
)

Upvotes: 1

Related Questions