tomtom92 tomtom92 - 1 year ago 134
Python Question

'NoneType' object has no attribute, reference class variable declared before __init__

I am using Flask with SQLAlchemy to create a 1-to-1 relationship between a Search request and parsed Data. Once a user has checked over the search results, they can click continue to parse the data (and create a Data class instance). However, I am unsure how to reference class variables before an

reference. I want my
instance to have a requests response object

Calling code (

search = Search(form) # create source result = g.user


data = Data()
data.search_id =

Search class (

class Search(db.Model):
id = db.Column(db.Integer, primary_key=True)
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey(''))

data_id = db.relationship('Data', backref='source', lazy='dynamic')

Data class (

class Data(db.Model):
id = db.Column(db.Integer, primary_key=True)
search_id = db.Column(db.Integer, db.ForeignKey(''))
search = db.relationship('Search', backref="information")
r = None

def __init__(self):
self.r = requests.get(

The error I get is:

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

It is clear that I cannot create
before the
variable is declared, since the error suggests that it is
type. I am not sure what the proper format creating new variables like this should be. The code works if I move the
declaration to a class method that is called after initialization. Thanks!

Answer Source

A few recommendations

The search attributes actually is declared, it's just not set. If it wasn't declared, you would get an AttributeError.

Call the superclass __init__. This is just good practice for all classes

def __init__(*args, **kwargs):
    super(Data, self).__init__(*args, **kwargs)

You could compute the value lazily

class Data(...):
    def __init__(...):
        self._r = None

    def r(self):
        if self._r is None and is not None:
            self._r = requests.get(
        return self._r
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download