title: Python Flask App building social network
body: <p>I am currently learning Python and trying to create a social network.  I am not able to view other streams of people who have made posts on the wall.  I get the following error <code>AttributeError: 'User' object has no attribute 'models'</code>.</p>
  
  <p>app.py</p>
  
  <pre><code>@app.route('/stream')
  @app.route('/stream/&lt;username&gt;')
  def stream(username=None):
      template = 'stream.html'
      if username and username != current_user.username:
          try:
              user = models.User.select().where(models.User.username**username).get()
  
  
          except models.DoesNotExist:
              abort(404)
          else:
              stream = user.posts.limit(100)
  
  
      else:
          user = current_user
          stream = current_user.models.get_stream().limit(100)
  
  
      if username != username:
          template = 'user_stream.html'
  
      return render_template(template, stream=stream, user=user)
  </code></pre>
  
  <p>model.py</p>
  
  <pre><code>class User(UserMixin, Model):
      username = CharField(unique=True)
      email = CharField(unique=True)
      password = CharField(max_length=100)
      joined_at = DateTimeField(default=datetime.datetime.now)
      is_admin = BooleanField(default=False)
  
      class Meta:
          database = DATABASE
          order_by = ('-joined_at',)
  
          def get_posts(self):
              return Post.select().where(Post.user == self)
  
          def get_stream(self):
              return Post.select().where(
                  (Post.user &lt;&lt; self.following()) |
                  (Post.user == self)
              )
  
          def following(self):
              """The users we are following"""
              return (
                  User.select().join(
                      Releationship, on=Releationship.to_user
                  ).where(
                      Releationship.from_user == self
                  )
              )
          def followers(self):
              """get users following"""
              return (
                  User.select().join(
                      Releationship, on=Releationship.from_user
                  ).where(
                      Releationship.to_user == self
                  )
              )
  
  
      @classmethod
      def create_user(cls, username, email, password, admin=False):
          try:
              cls.create(
                  username=username,
                  email=email,
                  password=generate_password_hash(password),
                  is_admin=admin
              )
  
          except IntegrityError:
              raise ValueError("user already exists")
  
  class Post(Model):
      timestamp = DateTimeField(default=datetime.datetime.now)
      user = ForeignKeyField(
          rel_model=User,
          related_name='posts'
  
      )
      content = TextField()
  
      class Meta:
          database = DATABASE
          order_by = ('-timestamp',)
  
  class Releationship(Model):
      from_user = ForeignKeyField(User, related_name='relationships')
      to_user = ForeignKeyField(User, related_name='related_to')
  
      class Meta:
          database = DATABASE
          indexes = (
              (('from_user', 'to_user'), True)
  
          )
  </code></pre>
  
  <p>stream.html</p>
  
  <pre><code>{%  extends "stream.html" %}
  
  {%  block content %}
  
  &lt;div class="row"&gt;
      &lt;div class="grid-25"&gt;
          &lt;h1&gt;{{ user.username }}&lt;/h1&gt;
      &lt;/div&gt;
  
  &lt;div class="grid-50"&gt;
  
      &lt;div class="grid-33"&gt;
          &lt;h5&gt;Posts&lt;/h5&gt;
          &lt;p&gt;{{ user.posts }}&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="grid-33"&gt;
  
          &lt;h5&gt;Followers&lt;/h5&gt;
      &lt;p&gt;{{ user.followers }}&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div class="grid-33"&gt;
          &lt;h5&gt;Following&lt;/h5&gt;
          &lt;p&gt;{{ user.following }}&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="grid-25"&gt;
         {%  if current_user.is_authenticated() %}
          {%  if user != current_user %}
          {%  if not user in current_user.following() %}
              &lt;a href="{{ url_for('follow', username=user.username) }}" class="small"&gt;Follow&lt;/a&gt;
              {% else %}
              &lt;a href="{{ url_for('unfollow', username=user.username) }}" class="small"&gt;UnFollow&lt;/a&gt;
          {%  endif %}
      {%  endif %}
      {%  endif %}
  
      &lt;/div&gt;
  
  &lt;/div&gt;&lt;/div&gt;
  
  {{ super() }}
  {% endblock %}
  </code></pre>