简体   繁体   English

在 Django 查询集中出现 _set 错误,object 没有属性

[英]getting error with _set in Django Query-Set, object has no attribute

I use _set to make ManyToMany relationship between customer and Service but endup having Error:我使用_set客户服务之间建立ManyToMany关系,但最终出现错误:

AttributeError at /customer/1/ /customer/1/ 处的 AttributeError
'carOwner' object has no attribute 'serviceOrderX_get' 'carOwner' object 没有属性 'serviceOrderX_get'

Is there anyone who want to help me..有没有人想帮帮我..

models.py模型.py

...

class carOwner(models.Model):
    name_X = models.CharField( max_length=150, null=True)
    email_X = models.CharField( max_length=150, null=True)
    phoneNo_X =models.CharField( max_length=150, null=True)

    def __str__(self):
        return self.name_X

class serviceOrderX(models.Model):
    CATEGORES = (...)
    carName_X = models.CharField( max_length=150, null=True)
    carNO_X = models.CharField( max_length=150, null=True)
    carOwner_X = models.ManyToManyField(carOwner)
    catagores_X = models.CharField(max_length=200,null=True,choices=CATEGORES)
    price_X = models.IntegerField(  null=True)
    service_X = models.ManyToManyField(orderService)

    def __str__(self):
        return str(f'{self.carName_X} , {self.carNO_X}')

views.py视图.py

...

def CustomerX(request , pk):
    customersX = carOwner.objects.get(id=pk)
    cuter = customersX.serviceOrderX_set.all()

    contX = {
        'customer' : cuter , 
    }
    return render(request, 'customer.html' , contX)

I think you just have a typo in your code.我认为您的代码中只是有一个错字。 It should be set instead of get -> customersX.serviceOrderX_set.all()应该set它而不是get -> customersX.serviceOrderX_set.all()

See https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/

You can also set a related_name on your ManyToManyField like service_orders so that you can do customersX.service_orders.all()您还可以在您的 ManyToManyField 上设置一个related_name ,例如service_orders ,以便您可以执行customersX.service_orders.all()

Btw.顺便提一句。 just a hint, using camel case for your fields in python is not a good style.只是一个提示,在 python 中为您的字段使用驼峰式大小写并不是一种好的风格。 Just do car_owner_x and for your class names use eg `ServiceOrderX https://peps.python.org/pep-0008/只需执行car_owner_x并为您的 class 名称使用例如 ` ServiceOrderX https://peps.python.org/pep-0008/

If you want to relate field, the first argument you should add is name of model you want to relate.如果你想关联字段,你应该添加的第一个参数是你想要关联的 model 的名称。 In your case you can fix this在您的情况下,您可以解决此问题

class serviceOrderX(models.Model):
    # change here
    service_X = models.ManyToManyField(carOwner)

read more here: https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/在这里阅读更多: https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'Registered_Courses' 对象没有属性 'course_set' Django - 'Registered_Courses' object has no attribute 'course_set' Django '问题' object 没有属性 'choice_set' django - 'Question' object has no attribute 'choice_set' django 错误:“设置”对象没有属性。-Python3 - Error: 'set' object has no attribute.. - Python3 我在 python 中收到此错误:“AttributeError: 'str' object has no attribute 'set'”。 我正在使用 tkinter,我该如何解决这个问题? - I'm getting this error in python: “AttributeError: 'str' object has no attribute 'set'”. I'm using tkinter, how do I fix this? django unittest 中的错误:“对象没有属性”对象 - Error in django unittest: 'object has no attribute' object “AttributeError:‘function’对象没有设置属性”tkinter - "AttributeError: 'function' object has no attribute set" tkinter AttributeError:“ CollectionReference”对象没有属性“ set” - AttributeError: 'CollectionReference' object has no attribute 'set' “ WebElement”对象没有属性“ set_value” - 'WebElement' object has no attribute 'set_value' AttributeError: 'set' 对象没有属性 'to_csv' - AttributeError: 'set' object has no attribute 'to_csv' “超级”对象没有属性“set_context” - 'super' object has no attribute 'set_context'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM