1👍
✅
You just need:
user = "KalleAnka" # exist as instance if User
t, created = Tweet.objects.get_or_create(from_user=user)
t.text = "hullabaloo my friend!"
t.to_user = "KajsaAnka"
t.save()
You always pass an instance of the model for a ForeignKey field when executing an ORM call.
I made a sample project at: https://www.dropbox.com/s/z6pb4rbz0iyol21/tweets_app.zip?dl=0
I tweaked your model code slightly not to collide with Django’s User model, here are the results I get:
>>> from tweets.models import Tweet, TwitterUser
>>> twitter_user = TwitterUser.objects.get(screen_name='KalleAnka')
>>> t, created = Tweet.objects.get_or_create(from_user=twitter_user)
>>> t.text = "hullabaloo my friend!"
>>> t.to_user = "KajsaAnka"
>>> t.save()
>>> t
<Tweet: Tweet object>
>>> t.from_user
<TwitterUser: TwitterUser object>
Source:stackexchange.com