django - get() returned more than one topic

前端 未结 6 2216
不知归路
不知归路 2020-11-30 02:30

When I tried to relate an attribute with another one which has an M to M relation I received this error:

get() returned more than one

6条回答
  •  甜味超标
    2020-11-30 02:37

    get() returned more than one topic -- it returned 2!

    The above error indicatess that you have more than one record in the DB related to the specific parameter you passed while querying using get() such as

    Model.objects.get(field_name=some_param)
    

    To avoid this kind of error in the future, you always need to do query as per your schema design. In your case you designed a table with a many-to-many relationship so obviously there will be multiple records for that field and that is the reason you are getting the above error.

    So instead of using get() you should use filter() which will return multiple records. Such as

    Model.objects.filter(field_name=some_param)
    

    Please read about how to make queries in django here.

提交回复
热议问题