python / django中setattr和对象操作之间的区别 [英] Difference between setattr and object manipulation in python/django

查看:217
本文介绍了python / django中setattr和对象操作之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

class Ticket(models.Model):
    title = models.CharField()
    merged_to = models.ForeignKey("self", related_name='merger_ticket', null=True, blank=True)
    looser_ticket = models.BooleanField(default=False)

有两种处理模型的方法:

There are couple of ways of manipulating the model:

第一

ticket = Ticket.objects.get(pk=1)
ticket.title = "This is edit title"
ticket.merged_to_id = 2
ticket.looser_ticket = True

第二

ticket = Ticket.objects.get(pk=1)
setattr(ticket, "title", "Edit Title")
setattr(ticket, "merged_to_id", 2)
setattr(ticket, "looser_ticket", True)

当我操作这些东西时,在布尔值更新的视图中,第一种方法无效,但是第二种方法有效。第一次使用和第二次使用以及何时应使用它们有什么区别?

When I was manipulating the stuff, in views for the boolean value updation the first method did not work, however the second method worked. What is the difference between using first and second, and when they should be used?

谢谢!

推荐答案

这更多是Python问题。

This is more of a Python question.

Python是一种非常动态的语言。您可以提前编写代码(类),或者Python允许您在运行时完全动态地创建类。考虑以下简单向量类的示例。您可以提前创建/编码类,例如:

Python is very dynamic language. You can code things (classes) ahead of time, or Python allows you to create classes completely dynamically at run-time. Consider the following example of a simple vector class. You can create/code the class ahead of time like:

class MyVector(object):
    x = 0
    y = 0

或者您可以通过以下方式动态创建类:

or you can create the class dynamically by doing:

fields = {'x':0, 'y':0}
MyVector = type('MyVector', (object,), fields)

方法之间的主要区别在于,对于一​​个方法,您提前知道了类的属性,而对于您可以想象的第二种方法,可以以编程方式创建字段字典,因此可以完全动态地创建类。

The main difference between the methods is that for one you know the class attributes ahead of time, whereas for the second method as you can imagine, you can programmatically create the fields dictionary, therefore you can create completely dynamically class(es).

因此,当您提前知道类的属性时,可以使用对象符号设置类属性:

So when you know the attributes of the class ahead of time, you can set class attributes using the object notation:

instance.attribute = value

请记住等效于:

instance.__setattr__("attribute", value)

但是,在某些情况下,您可能不知道需要提前处理的类属性。在这里可以使用 __ setattr __ 函数。但是,不建议您这样做。因此,建议使用Python的内置方法 setattr ,该方法在内部调用 __ setattr __ 方法:

However there are scenarios where you don't know the class attributes you will need to manipulate ahead of time. This is where you can use __setattr__ function. However it is not recommended practice. So instead the recommendation is to use Python's build-in method setattr which internally calls the __setattr__ method:

setattr(instance, attribute, value)

使用这种方法,您可以提前设置未知的属性,甚至可以循环一些 dict 并从dict中设置值:

Using this approach you can set attributes you don't know ahead of time or you can even loop of some dict and set values from dict:

values = {
    'title': 'This is edit title',
    ...
}
for k, v in values.items():
    setattr(ticket, k, v)

不确定常规表示法为何不起作用。可能与您用来设置属性的方法无关。

Not sure why the regular notation did not work for. It probably has nothing to do with the method you used to set attributes.

这篇关于python / django中setattr和对象操作之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆