0

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?

fcp
  • 89
  • 9

2 Answers2

1

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
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
  • Thanks for the pointer. So the print statement below should in turn otuput the respective data? – fcp Aug 09 '18 at 07:42
  • Thanks, that solved my error. How can I access a particular value of 'date', since various methods and the answer below still result in errors. Thanks! – fcp Aug 09 '18 at 08:04
  • When I do as you suggested to print all the dates I get no error, but when I try to print only the most recent date I get No Attribute Error as above. It must be how I querying. This results in an error: `lines[0].date` – fcp Aug 09 '18 at 08:26
  • @fcp can you please post full query you are trying to execute ? – Nishant Nawarkhede Aug 09 '18 at 08:30
  • I am trying `last_oee = Test.objects.latest('oee')` with error: `no such column: Test.id` – fcp Aug 09 '18 at 09:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177710/discussion-between-nishant-nawarkhede-and-fcp). – Nishant Nawarkhede Aug 09 '18 at 09:49
0

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Som-1
  • 601
  • 7
  • 16
  • I still get an error: `no such column: LineOEE1.id` when I run `dates = Lineoee1.objects.values_list('id', 'date')` – fcp Aug 09 '18 at 07:46