Why .filter() in django returns duplicated objects?

后端 未结 3 598
悲&欢浪女
悲&欢浪女 2021-01-17 12:03

I\'ve followed django tutorial and arrived at tutorial05.

I tried to not show empty poll as tutorial says, so I added filter condition like this:

cla         


        
3条回答
  •  借酒劲吻你
    2021-01-17 12:41

    Because you created two objects with same properties. If you want to ensure uniqueness, you should add validation in clean and add unique index on identifier field too.

    Besides filter returns all the objects that match the criteria, if you are expecting only one item to be returned, you should use get instead. get would raise exception if less or more than 1 item is found.

提交回复
热议问题