1

I've been searching but can't find anything ...

My model is,

class Departament(db.Model):

    id = Column(Integer, primary_key=True)
    departament = Column(String, unique=True)
    depmv = relationship('Mvpordep', backref='line', lazy='select')
    depfac = relationship('Bill', backref='line_fac', lazy='select')

    def __init__(self, departament):

        self.departament = departament

    def __repr__(self):
        return '{}'.format(self.departament) 

and y try

departament = Departament.query.filter_by(departament='ti').first()

print(departament.id)

but it gives me the following error

AttributeError: 'NoneType' object has no attribute 'id'

When I get multiple rows and iterate it with a for loop, no problem. But if I try to do it with a for loop, the logical thing happens

for out in departament:
    print(out.id)
TypeError: 'NoneType' object is not iterable

I don't understand what happens

4
  • Change this line from 'Departament.query.filter_by(departament='ti').first()' to 'Departament.objects.filter_by(departament='ti').first()' Commented Aug 12, 2021 at 3:34
  • No luck AttributeError: type object 'Departament' has no attribute 'objects' Commented Aug 12, 2021 at 6:48
  • Your actual model name is Departmento not only Department there is an ‘o’ in the last Commented Aug 12, 2021 at 6:54
  • Yes, you are correct, excuse me. It was an error when writing the post. Thank you very much for the tip. Finally I have already found the problem. It was in that the dev database and the production one were different, and that data did not exist in dev. Thank you very much for your help! Commented Aug 12, 2021 at 12:47

1 Answer 1

2

You are using __repr__() function in your department class. So if you fetch data from department class you will only get data of department(as you created).

Here the error is, there is no id in your fetched data it's only department. So that why you get Nonetype error.

If you want all data from department class, remove __repr__() function (and __init__()) from department class or declare every column name in your __repr__() function (and __init__()).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.