Django: check whether an object already exists before adding

后端 未结 5 773
半阙折子戏
半阙折子戏 2021-01-31 17:19

This is a pretty simple Django question, but I can\'t find the answer in the Django docs, despite lots of hunting!

How do I check whether an object already exists, and o

5条回答
  •  故里飘歌
    2021-01-31 17:51

    If you're using a recent version of Django, you can use the unique_together option to the UserToUserRole model, and then use the get_or_create() method Joe Holloway showed. This will guarantee that you can't get a duplicate.

提交回复
热议问题