如何使用 Django 将 python 类存储到数据库中?

发布于 2024-08-20 15:41:52 字数 821 浏览 8 评论 0原文

我有两个文件:

choices.py

class SomeChoice:
    name = u"lorem"

class AnotherChoice:
    name = u"ipsum"

# etc...

models.py

from django.db import models
import choices

class SomeModel(models.Model):
    CHOICES = (
        (1, choices.SomeChoice.name),
        (2, choices.AnotherChoice.name),
        # etc...
    )
    somefield = models.IntegerField('field', choices=CHOICES)

问题:choices.py 中的类需要类似的东西要存储在我的数据库中的主键。这里我用手写了这些键(1, 2, ...),但这很难看。

例如,我不想这样做:

class SomeChoice:
    id = 1
    name = "lorem"

class AnotherChoice:
    id = 2
    name = "lorem"

所以我的问题是:将 python 类存储到数据库中的最佳方法是什么

请原谅我糟糕的英语。如果您需要更多信息,请告诉我。 ;-)

I have two files:

choices.py

class SomeChoice:
    name = u"lorem"

class AnotherChoice:
    name = u"ipsum"

# etc...

models.py

from django.db import models
import choices

class SomeModel(models.Model):
    CHOICES = (
        (1, choices.SomeChoice.name),
        (2, choices.AnotherChoice.name),
        # etc...
    )
    somefield = models.IntegerField('field', choices=CHOICES)

The problem: classes from choices.py need something like a primary key to be stored in my database. Here I write these keys (1, 2, ...) by hand, but this is ugly.

For instance, I don't want to do that:

class SomeChoice:
    id = 1
    name = "lorem"

class AnotherChoice:
    id = 2
    name = "lorem"

So my question is: what is the best way to store python classes into a database ?

Please excuse my ugly english. If you need more informations, just tell me. ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2024-08-27 15:41:52

您可以使用 pickle 来存储类的实例,但这样会更难看,并且在这种情况下您不需要将类存储在数据库中,所以不要(您想要尽可能避免访问数据库)。

为了避免在两个地方重复 ID,您可以将代码更改为以下内容:

choices.py

_registry = {}

def register(choice_class):
    id = len(_registry) + 1
    choice_class.id = id
    _registry[id] = choice_class

def as_list():
    ret = []
    for id in sorted(_registry):
        ret.append((id, _registry[id].name))
    return ret

def get_choice(id):
    return _registry[id]

class SomeChoice:
    name = u"lorem"

class AnotherChoice:
    name = u"ipsum"

register(SomeChoice)
register(AnotherChoice)

models.py< /强>

from django.db import models
import choices

class SomeModel(models.Model):
    somefield = models.IntegerField('field', choices=choices.as_list())

You could use pickle to store instances of the classes, but then it would be uglier, and you don't need to store the classes in the database in this case, so don't (you want to avoid hitting the database as much as possible).

To avoid repeating the IDs in two places, you could change the code to something like that:

choices.py

_registry = {}

def register(choice_class):
    id = len(_registry) + 1
    choice_class.id = id
    _registry[id] = choice_class

def as_list():
    ret = []
    for id in sorted(_registry):
        ret.append((id, _registry[id].name))
    return ret

def get_choice(id):
    return _registry[id]

class SomeChoice:
    name = u"lorem"

class AnotherChoice:
    name = u"ipsum"

register(SomeChoice)
register(AnotherChoice)

models.py

from django.db import models
import choices

class SomeModel(models.Model):
    somefield = models.IntegerField('field', choices=choices.as_list())
人事已非 2024-08-27 15:41:52

SomeChoice 和 AnotherChoice 类的价值是什么?为什么不将键和值存储在字典中(SomeModel 中的一种链接 CHOICES)并创建一个仅代表一种选择的新类,

class UserChoice:
    def __init__(self, id, name):
        self.id = id
        self.name = name

然后您将获得与 SomeChoice 和 AnotherChoice 相同的功能,但如果您添加更多选择,你不需要更多的课程。也许您的示例过于简单,但我没有看到这些类的价值。抱歉,如果我完全没有抓住要点。

What's the value of the SomeChoice and AnotherChoice classes? Why not just store the keys and values in a dictionary (kind of link CHOICES in your SomeModel) and have one new class that just represents a choice,

class UserChoice:
    def __init__(self, id, name):
        self.id = id
        self.name = name

and then you get the same functionality of your SomeChoice and AnotherChoice, but if you add more choices, you don't need more classes. Maybe your example was just over-simplified, but I don't see the value of those classes. Sorry if I missed the point completely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文