*_set attributes on Django Models

后端 未结 4 1518
梦毁少年i
梦毁少年i 2021-02-04 03:09

I have a very basic question about django.db.models.

In this official django tutorial, if you search for word \"choice_set\", you will see tha

4条回答
  •  广开言路
    2021-02-04 04:03

    In the Document you are referring https://docs.djangoproject.com/en/1.2/intro/tutorial01/#intro-tutorial01

    '_set' refers to the all the choices that relates to the Poll object, Since the Choice model has poll that is a Foreign Key to Poll of ID.

    For Example:

    Consider Poll table row(1,'How are you?','2014 7 3')

    and Choice table

    when you say

    p = Poll.objects.get(id=1)
    

    //now p is a Poll Object that holds <1,'How are you?','2014 7 3'> p.choice_set.create(choice="1st",votes=8) // in Choice table it creates a row that refers poll ID 1 i.e Choice table row(1,1,'1st',8)

    Basically _set refers to the tuples of the Choice table (poll) referring Poll table (ID)

提交回复
热议问题