2

我有一個可以分配給模型AttributeError的:「ManyToManyDescriptor」對象有沒有屬性「所有」 - Django的

我打過電話來得到什麼團體的響應也該員工屬於許多其他組工作人員模型但我不斷收到錯誤。

有人能請我幫忙嗎? 用戶模型

class Staff(Model): 
    groups = ManyToManyField(Group, 
          related_name="%(class)ss", 
          related_query_name="%(class)s", 
          blank=True) 

class Group(Model): 
    creator = ForeignKey(Employer, 
         on_delete=CASCADE, 
         related_name="%(class)ss", 
         related_query_name="%(class)s") 
    group_name = CharField(max_length=256, unique=True) 
    created_at = DateTimeField(auto_now_add=True) 

我已經嘗試了一些方法,如

staff = Staff.objects.filter(pk=1) groups = staff.group.all()groups = staff.group_set.all()groups = staff.group.filter()

,而且我不記得一些其他的方式,但我不斷獲取錯誤。

預先感謝

+1

它是組。所以make groups = staff.groups.all() – sebb

+0

@sebb我會得到像'AttributeError'這樣的錯誤:'QuerySet'對象沒有屬性'groups'' – Dora

回答

4

Django的過濾器()返回一個查詢集對象,這是結果的容器。因此,您需要在嘗試訪問字段之前選擇特定的結果對象。

results = Staff.objects.filter(pk=1) 
for staff in results: 
    print staff.groups.all() 
相關問題