Django模型类和自定义属性 [英] Django model class and custom property

查看:148
本文介绍了Django模型类和自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Django中有一个模块类,并添加了一个不被保存到数据库中的自定义属性因此不在模型结构中表示:

  class Category(models.Model):
groups = models。 ManyToManyField(Group)
title = defaultdict()

现在,当我在shell或者写一个测试,我做了以下操作:

  c1 = Category.objects.create()
c1。 title ['de'] ='german title'
print c1.title ['de']#printsgerman title

c2 = Category.objects.create()
print c2.title ['de']#printsgerman title< - WTF?

似乎标题是全球性的。如果我将标题改为一个简单的字符串,它按预期工作,所以它必须用dict做某事?我也尝试将标题设为属性:

  title = property(_title)

但这并没有起作用。那么我该怎么解决呢?谢谢你提前!



编辑:



这是基础问题的意图,为您提供更好的根据要求查看周围的环境:
在我们的模型结构中,我们有一个存储翻译的模型类。这个类与所有其他具有彼此关系的类没有绑定。翻译类存储翻译的值,语言键,翻译键和翻译所属的包和类。一些模型类可以具有可以转换成不同语言的属性。这些属性没有映射到django模型结构中,因为这在我们的眼中并不是真的可能。每个具有可翻译属性的类,我们称之为可翻译,可以有一个或多个这些属性。这是翻译键。例如。如果存在具有可翻译属性title的类Category,则模型转换将module.somewhere.Category存储为包/类,标题作为翻译键,德文翻译值分类和语言键de。
我的目的是为了轻松访问此属性。所以所有这些模型类继承自一个简单的类Translatable。它具有解析模块路径和类的名称(用于稍后存储在翻译数据库表中)的方法和使用属性名称的_propertize方法。适当化实例化每个可翻译属性名称唯一的类翻译器。该类从翻译模型类中解析真实的翻译值,并且自动解析当前选择的语言的翻译的一些东西。

解决方案

不要这样做。您的标题属性完全是全局。它是课程的一部分,而不是每个实例的一部分。



执行此操作。

  class Category(models.Model):
groups = models.ManyToManyField(Group)
@property
def title(self):
return self._title
def save(self,* args,** kw):
try:
self._title
除了AttributeError:
self._title = defaultdict()
super(Category,self).save(* args,** kw)

如果可以定义您的实际用例,可能可以简化这一点。


Howdy - today a weird problem occured to me:

I have a modle class in Django and added a custom property to it that shall not be saved into the database and therefore is not represent in the models structure:

class Category(models.Model):
    groups = models.ManyToManyField(Group)
    title = defaultdict()

Now, when I'm within the shell or writing a test and I do the following:

c1 = Category.objects.create()
c1.title['de'] = 'german title'
print c1.title['de'] # prints "german title"

c2 = Category.objects.create()  
print c2.title['de'] # prints "german title" <-- WTF?

It seems that 'title' is kind of global. If I change title to a simple string it works as expected, so it has to do something with the dict? I also tried setting title as a property:

title = property(_title)

But that did not work, too. So, how can I solve this? Thank you in advance!

EDIT:

Here is the intention of the base problem to provide you a better look to the whole surrounding enviroment as requested: In our model structure we have a model class that stores translations. This class is unbound from all the other classes that have relations between each other. The translation class stores the translated value, a language key, a translation key and the package and class the translation belongs to. Some model classes can have properties that can be translated into different languages. These properties are not mapped within the django model structure as this is not truely possbile in our eyes. Each of this classes with translatable properties, let's call them translatables, can have one or more of these properties. That's the translation key is for. E.g. if there is a class Category with a translatable property "title", the model translation will store "module.somewhere.Category" as package/class, "title" as translation key, and e.g. for german the translation value "Kategorie" and the language key "de". My aim is to ease the access to this properties. So all this model classes inherit from a plain class called "Translatable". It has a method for resolving the module path and name of the class (for the later storing within the translation database table) and a "_propertize" method that takes the name of the property. Propertize instantiates a class "Translator" that is unique for each translatable property name. This class does the resolving of the real translation value from the translation model class and some stuff for automatically resolving the translation of currently chosen language.

解决方案

Don't do it that way. Your title attribute is completely "global". It's part of the class, not part of each instance.

Do something like this.

class Category(models.Model):
    groups = models.ManyToManyField(Group)
    @property
    def title(self):
        return self._title
    def save( self, *args, **kw  ):
        try:
            self._title
        except AttributeError:
            self._title= defaultdict()
        super( Category, self ).save( *args, **kw )

If you could define your actual use case, it might be possible to simplify this a great deal.

这篇关于Django模型类和自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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