我如何在Django中将〜[0]加到〜[1]?

时间:2018-09-02 09:32:34

标签: python django web

  1. 代码:

    >>> from test2.models import Member_info
    
    >>> member = Member_info.objects.all()
    
    >>> member
    <QuerySet [<Member_info: John2>, <Member_info: John1>, <Member_info: John3>]>
    
    >>> member = Member_info.objects.all()[0]
    
    >>> member
    <Member_info: gun2>
    
    >>> member += Member_info.obejcts.all()[1]
    
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    AttributeError: type object 'Member_info' has no attribute 'obejcts'
    
    >>> member += Member_info.objects.all()[1]
    
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    TypeError: unsupported operand type(s) for +=: 'Member_info' and 'Member_info'
    
    >>> member.append(Member_info.objects.all()[1])
    
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
    AttributeError: 'Member_info' object has no attribute 'append'
    

2。如何添加、、、?

1 个答案:

答案 0 :(得分:0)

您可以附加到list,但是您尝试附加到Member_info。试试这个:

member = Member_info.objects.all()[0]
members = [member]
members.append(Member_info.objects.all()[1])