Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Build a Social Network with Flask Broadcasting Post Form and View

Kamaren McCord
Kamaren McCord
11,236 Points

Cannot use the post model, help please

I keep getting this 'User' object has no attribute '__get_current_object' arttibute error traceback. Am i missing something or has this method been depreciated?

This is my post view:

@app.route('/new_post', methods=('GET','POST'))
@login_required
def post():
    #post has an error when attempting
    form = forms.PostForm()
    if form.validate_on_submit():
        models.Post.create(user=g.user.__get_current_object(),
                            content = form.content.data.strip())
        flash('message posted, thanks!', 'sucess')
        return redirect(url_for('index'))
    return render_template('post.html', form=form)

and my post model:

class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(
        User,
        related_name = 'posts'
    )
    content = TextField()

    class Meta:
        database = DATABASE
        order_by = ('-timestamp',)

4 Answers

I'm not sure I know enough to help you here but it looks like you begin _get_current_object() with two underscores where there should only be one.

nakalkucing
nakalkucing
12,964 Points

Good catch KRIS NIKOLAISEN! Kamaren McCord, that's all I can find in your provided code too. Happy coding! :)

nakalkucing
nakalkucing
12,964 Points

Hi Kamaren McCord! The error is in your Post model. You're missing something important before one of your attributes:

class Post(Model):
    timestamp = DateTimeField(default=datetime.datetime.now)
    user = ForeignKeyField(
        User,   <--error here!
        related_name = 'posts'
    )

It should be:

    rel_model= User,

Happy Coding! :)

let me know if this doesn't solve the issue. I'd be happy to help.

Kamaren McCord
Kamaren McCord
11,236 Points

Alright, Sooo I added the rel_model to my post model, How ever i seem to still be getting this error for the attribute error getcurrent_object.

Kamaren McCord
Kamaren McCord
11,236 Points

Thank you Kris Nikolaisen, It's always those underscores, I did not realize it was not a magic method.