3

I have created a simple Django application which tries to access table named fruits present in PostgreSQL database and display its contents. I followed the official Django documentation and implemented below code as far as I understand. But below procedure is incomplete I believe as fruits.objects.all() is failing to fetch any data. I am missing something can anybody point it out any help would be appreciated.

Table: fruits

enter image description here

models.py

from django.db import models

class fruits(models.Model):
    ID = models.CharField(max_length=20)
    Name = models.CharField(max_length=20)

    def __str__(self):
        return self.Name

views.py

from database.models import fruits

def index(request):
    data = fruits.objects.all()

    print(len(data))

    for item in data:
        print(item)

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '<database_name>',
        'USER': '<user_name>',
        'PASSWORD': '<password>',
        'HOST': '<host_name>',
        'PORT': '',
    }
}
1
  • 1
    Class names are by convention writen with capital letter and are singular nouns. Thus class fruits should become class Fruit. Class attributes are by convention written in snake_case, thus id instead of ID, name instead of Name. Check PEP 8 and follow coding convetions. Commented Feb 24, 2018 at 13:24

1 Answer 1

1

By default Django will create new table appname_modelname for model. So in your case this queryset fruits.objects.all() fetch data from app_fruits table, not fruits. You can specify table name using db_table meta option. For example to link model fruits with existing table fruits add following Meta to the model:

class fruits(models.Model):
    ID = models.IntegerField(max_length=20)
    Name = models.CharField(max_length=20)

    class Meta:
        db_table = 'fruits'

    def __str__(self):
        return self.Name

UPD

Django expect id field to be primary key by default not ID, to change this add primary_key option to ID field:

ID = models.IntegerField(max_length=20, primary_key=True)
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting this error now django.db.utils.ProgrammingError: column fruits.id does not exist LINE 1: SELECT "fruits"."id", "fruits"."Name" FROM "fruits"
@OliviaBrown add primary_key=True to ID field: ID = models.CharField(max_length=20, primary_key=True).
I would strongly suggest against using CharField as primary key. It's a bad practice. Use rather surrogate keys.
@cezar thanks I missed this part. In DB primary key is integer field. So it should be integer instead of char field in model also.
how can I access a table that wasn't made using model.Models, let's say I used the command line to make a table, and add items to that table. how can I access those items?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.