fcp
fcp

Reputation: 89

Django 1.10 Legacy Database - Unable to Retrieve Objects

I have a very small (so far) legacy database which I auto-generate in Django using

python manage.py inspectdb > models.py

I put this in my python package that holds the app and added to my INSTALLED_APPS setting.

I ran: python manage.py makemigrations app_name

Migrations for 'app_name': app_name/migrations/0002_auto_20180809_0453.py

Next: python manage.py migrate app_name

Operations to perform: Apply all migrations: app_name Running migrations: Applying app_name.0001_initial... OK Applying app_name.0002_auto_20180809_0453... OK

I checked that the models were created using: python manage.py sqlmigrate app_name 0001, which showed that several models were created.

My models.py file looks like this:

from __future__ import unicode_literals

from django.db import models


class Test(models.Model):
    date = models.FloatField(blank=True, null=True)
    shift = models.FloatField(blank=True, null=True)
    timer = models.FloatField(blank=True, null=True)
    target_produce = models.FloatField(blank=True, 
null=True)
    actual_produce = models.FloatField(blank=True, 
null=True)
    oee = models.FloatField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'Test'

I am trying to access my database from views.py. Here is my code:

File: views.py                              

from __future__ import unicode_literals

from django.shortcuts import render
from django.http import HttpResponse

from lineoee.models import Test

# Create your views here.
def index(request):
        context = {}

        lines = Test.objects.date() # <-- Error here
        print(lines)
        return render(request, 
       'lineoee/index.html',context)

I get the atribute error

'Manager' object has no attribute 'date'

I tried to implement the solution here and added the UserManager import and objects = UserManager() but I ended up with

'Usermanager' object has no attribute 'date'

Any suggestions on how I can eliminate this error and access data from my legacy database correctly?

Upvotes: 0

Views: 192

Answers (2)

Som-1
Som-1

Reputation: 661

An "objects" object is not the data itself. As Django tells you, it is a manager which allows you to access the data. In order to get data from it you should use one of its methods: get() for a single data entry, all() or filter() or exclude() for a list of entries. E.g.

lines = Lineoee1.objects.all()   # retrieves all data from Lineoee1 model
lines[0].date                    # retrieves date field from the first entry

Or if you want to get only dates, then try to use values_list() method with list of field names:

dates = Lineoee1.objects.values_list('id', 'date')

Added 'id' as an extra field to identify which entry does dates belong to - you may remove or replace it with whatever primary key you have.

More info on querying at django docs

Upvotes: 0

Nishant Nawarkhede
Nishant Nawarkhede

Reputation: 8400

You need to do it as,

Test.objects.all().values('date')

If you need to print respective data,

for i in Test.objects.all().values('date'):
    print i['date']

or

for i in Test.objects.all().values_list('date', flat=True):
    print i

Upvotes: 1

Related Questions