在django模型领域做属性工作? [英] do properties work on django model fields?

查看:108
本文介绍了在django模型领域做属性工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为提出这个问题的最好方法是使用一些代码,我可以这样做吗? (编辑:ANSWER:否)



  
class MyModel (models.Model):

foo = models.CharField(max_length = 20)

bar = models.CharField(max_length = 20)

  def get_foo(self):
if self.bar:
return self.bar
else:
return self。 foo

def set_foo(self,input):
self.foo = input

foo = property(get_foo,set_foo)




或者我必须这样做:



是的,你必须这样做:



  class MyModel(models.Model):
_foo = models.CharField(max_length = 20,db_column ='foo ')
bar = models.CharField(max_length = 20)

def get_foo(self):
如果self.bar:
return self.bar
else:
return self._foo

def set_foo(self,input):
self._foo = input

foo = property(get_foo,set_foo)

注意:您可以通过传递db_column将列名称保存为数据库中的foo到模型领域。这是非常有用的,当你在现有的系统上工作,而且你不想自己做数据库迁移没有任何理由

解决方案

p>一个模型字段已经是财产了,所以我会说你必须做第二种方式来避免名字冲突。



当你定义foo = property(。 。)它实际上覆盖了foo = models ..行,所以该字段将不再可访问。



您将需要为该属性使用不同的名称,领域。实际上,如果你在实例#1中这样做,那么当你尝试访问这个属性时,你会得到一个无限循环,因为它现在试图返回它。



<编辑:也许您也应该考虑不要使用_foo作为字段名称,而是使用foo,然后为属性定义另一个名称,因为属性不能在QuerySet中使用,因此当您需要使用实际的字段名称时做一个过滤器例如。


I think the best way to ask this question is with some code... can I do this? (edit: ANSWER: no)


    class MyModel(models.Model):
foo = models.CharField(max_length = 20)
bar = models.CharField(max_length = 20)

    def get_foo(self):  
        if self.bar:  
            return self.bar  
        else:  
            return self.foo  

    def set_foo(self, input):  
        self.foo = input  

    foo = property(get_foo, set_foo)  

or do I have to do it like this:

Yes, you have to do it like this:

class MyModel(models.Model):
    _foo = models.CharField(max_length = 20, db_column='foo')
    bar = models.CharField(max_length = 20)

    def get_foo(self):
        if self.bar:
            return self.bar
        else:
            return self._foo

    def set_foo(self, input):
        self._foo = input

    foo = property(get_foo, set_foo)

note: you can keep the column name as 'foo' in the database by passing a db_column to the model field. This is very helpful when you are working on an existing system and you don't want to have to do db migrations for no reason

解决方案

A model field is already property, so I would say you have to do it the second way to avoid a name clash.

When you define foo = property(..) it actually overrides the foo = models.. line, so that field will no longer be accessible.

You will need to use a different name for the property and the field. In fact, if you do it the way you have it in example #1 you will get an infinite loop when you try and access the property as it now tries to return itself.

EDIT: Perhaps you should also consider not using _foo as a field name, but rather foo, and then define another name for your property because properties cannot be used in QuerySet, so you'll need to use the actual field names when you do a filter for example.

这篇关于在django模型领域做属性工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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