Admin: Unknown fields

135 views
Skip to first unread message

Rolston Jeremiah

unread,
Sep 28, 2015, 6:56:42 PM9/28/15
to Django users
Hello,

I am new to Python and Django so please bear with me.

I have  created a model(below) and would now like to access 
from the admin interface. As per instructions  I found on the 
django site (Part 2 tutorial version 1.8.4) 


model
///////////////////////////////////////////////////////////////////////////////////////////////////////

class Genus(models.Model):
genus_id = models.AutoField(primary_key=True),
scientific_genus = models.CharField(max_length=32),
common_name = models.CharField(max_length=32),
common_examples = models.TextField(),
genus_choices = (
('Member Banana...','Musa'),
('Mango...','Magnifera'),
('Avocado...','Persea Americana'),
('Soursop, Custard, Sugar Apple...','Annona Reticulata'),
('Ginger, Tumeric ...','Zingiber Officinale Roscoe'),
('Persimmon....','Diospyros'),
('Breadfruit...','Artocarpus Altilis'),
('Cherry ...','Prunus'),
('Grapes ...','Vitis'), 
('Tomato ...','Lycopersicon'), 
('EggPlant ...','????????'), 
('Onion, Garlic ...','Allium'),
('Cashew ...','Anacardium'),
('Rice ...','Oryza'),
('Pepper ...','Capsicum'),
('Orange,Lemon, Lime ....','Citrus')
),
image_url = models.CharField(max_length=128),
genus = models.CharField(max_length=32,choices=genus_choices),
description = models.TextField()


I edit  "admin.py"  so that it now looks like


admin.py
//////////////////////////////////////////////////////////////////////////////////////////////////

from django.contrib import admin
from cfxBase.models import Genus
# Register your models here.


class GenusAdmin(admin.ModelAdmin):
    fields = [
               'genus',
               'scientific_genus',
               'common_name',
               'description',
               'common_examples',
               'image_url'
    ]
    
admin.site.register(Genus, GenusAdmin)


////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Now when I attempt the admin page add entry  for Genus I am getting this 
error:


FieldError at /admin/cfxBase/genus/add/

Unknown field(s) (scientific_genus, genus, common_name, common_examples, 
                  description, image_url) specified for Genus. 

Check fields/fieldsets/exclude attributes of class GenusAdmi


Your help will be  greatly appreciated









Gergely Polonkai

unread,
Sep 29, 2015, 2:14:04 AM9/29/15
to Django users

Hello,

I suspect you didn't make/run migrations:

python manage.py makemigrations
python manage.py migrate

The first command creat the so-called migration files that are required to update the database by adding new tables and fields, while the second one does the actual upgrade.

Also, allow me some comments.

You may want to omit genus_id from your model, as it gets created automatically anyway. One field less to manage while you write your code. Of course, it's up to you.

The second one is your choice field. By convention, Django people do it this way:

EXAMPLE_CHOICES = (
    ('short code to store in DB', 'long text to display'),
    …
)
field = models.CharField(choices=EXAMPLE_CHOICES)

My main point here other than conventions is that "short code" won't get displayed for the user, so make your choice list based on this.

Third, eggplant is Solanum if I recall well :)

Best,
Gergely

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3b131d1a-78b9-4294-adad-1040a5ab7a12%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rolston Jeremiah

unread,
Sep 29, 2015, 12:47:35 PM9/29/15
to Django users
Hello Polonkai,

Much thanks for responding to my cry for help!  I will make the
suggested changes and correctionns. Thanks much.


Actually, I did adhere  to Django's typical  workflow as you hinted to:


        python manage.py  makemigrations
        python manage.py migrate

But when I examined the 0001_initial.py file in the migrations folder:



from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='ActiveContracts',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='AddressBook',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='Agents',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='ArchivedContracts',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='Genus',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='ManagementUnits',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='ProductionUnits',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='Products',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
            ],
        ),
        migrations.CreateModel(
            name='SoilProfiles',
            fields=[
                ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
                ('comment', models.TextField(default='Not provided')),
            ],
        ),
    ]

ONLY the id field is being auto-generated. As my-level of Django skiils I am not
sure how identify the source of the problem. The models have correct names
so manage.py is "model-aware (assume correct env setup)". I was looking
here (https://realpython.com/blog/python/digging-deeper-into-migrations)  for more info on
migration and was considering to  edit  migration file to setup the database tables. BUT
I AM NOT CLEAR ON THE IMPLICATIONS GOING FORWARD FOR ADMIN INTERFACE
and so. I shall see where  this path leads to - hopefully a solution and a deeper understanding of
the Django framework.

Rjae
regards


Florian Schweikert

unread,
Sep 29, 2015, 1:45:00 PM9/29/15
to django...@googlegroups.com
On 29/09/15 18:47, Rolston Jeremiah wrote:
> class Genus(models.Model):
> genus_id = models.AutoField(primary_key=True),
> scientific_genus = models.CharField(max_length=32),
> common_name = models.CharField(max_length=32),
> common_examples = models.TextField(),

It's probably because of the ',' at each line end.
I think genus_id became a tuple of fields, remove them and try again :)

--
Florian

signature.asc

Rolston Jeremiah

unread,
Sep 30, 2015, 4:03:21 PM9/30/15
to Django users
Hello Florian Schweikert,

Thank you. The  commas (',')   were the problem. Consider this user-issue closed.
Great place to learn

Thanks to  all.
Rjae


On Monday, September 28, 2015 at 6:56:42 PM UTC-4, Rolston Jeremiah wrote:
Reply all
Reply to author
Forward
0 new messages