LoginSignup
1
0

More than 5 years have passed since last update.

DjangoのViewでデータを追加する時 Error:save() missing 1 required positional argument: 'self'

Posted at

DjangoのViewでデータを追加する時、例えばフォームから入力してデータを追加するのではなく、
View上でDjangoモデルに直接データを追加したい時がある。

これをやろうとした時に
少し詰まったのでメモしておく。

下のコードにあるように、FavoriteというDjangoモデルにuserとfavoriteのフィールドがあり、
そこにLikeしたユーザとその投稿の記事を紐づけたい時、初めはFav=Favoriteとしていた為、
エラーが生じていた。修正案としてFav=Favorite()としてインスタンスを引き渡すと無事に解決出来た。

qiita.rb
def like(request):
    Fav = Favorite()
    Fav.user = request.user
    Fav.favorite = post
    Fav.save()

また、こちらの記事にも
同じような事が記載されている。
https://stackoverflow.com/questions/46907511/typeerror-save-missing-1-required-positional-argument-self

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0