我目前正在学习Python并尝试创建一个社交网络。我无法查看在墙上发帖的其他人群。我收到以下错误AttributeError: 'User' object has no attribute 'models'
。
app.py
@app.route('/stream')
@app.route('/stream/<username>')
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)
model.py
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 << 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)
)
stream.html
{% extends "stream.html" %}
{% block content %}
<div class="row">
<div class="grid-25">
<h1>{{ user.username }}</h1>
</div>
<div class="grid-50">
<div class="grid-33">
<h5>Posts</h5>
<p>{{ user.posts }}</p>
</div>
<div class="grid-33">
<h5>Followers</h5>
<p>{{ user.followers }}</p>
</div>
<div class="grid-33">
<h5>Following</h5>
<p>{{ user.following }}</p>
</div>
<div class="grid-25">
{% if current_user.is_authenticated() %}
{% if user != current_user %}
{% if not user in current_user.following() %}
<a href="{{ url_for('follow', username=user.username) }}" class="small">Follow</a>
{% else %}
<a href="{{ url_for('unfollow', username=user.username) }}" class="small">UnFollow</a>
{% endif %}
{% endif %}
{% endif %}
</div>
</div></div>
{{ super() }}
{% endblock %}
答案 0 :(得分:2)
我认为你的缩进有问题。您的get_posts
,get_stream
,following
和followers
方法属于User
类,而不属于Meta
子类。现在你将它们放在Meta
子类中。将它们缩进到与Meta
子类相同的级别。
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 << self.following()) |
(Post.user == self)
)
def following(self):
""" The users we are following """
return (
User.select().join(
Relationship, on=Relationship.to_user
).where(
Relationship.from_user == self
)
)
def followers(self):
""" get users following """
return (
User.select().join(
Relationship, on =Relationship.from_user
).where(
Relationship.to_user == self
)
)
@classmethod
def create_user(cls, username, email, password, admin=False):
try:
with DATABASE.transaction():
cls.create(
username=username,
email=email,
password=generate_password_hash(password),
is_admin=admin)
except IntegrityError:
raise ValueError("user already exists")