简体   繁体   English

Django模型中的硬编码属性

[英]Hardcoding attributes in django models

OK here goes, this is one of those questions that makes perfect sense in my head but is difficult to explain properly :) I have a django app where I want to store records for lots of different items of equipment. 好的,这是在我看来很有意义但很难正确解释的问题之一:)我有一个django应用程序,我想在其中存储许多不同设备项的记录。 Each type of equipment will have a custom model to store its attributes, such as MyEquipment below. 每种类型的设备都有一个自定义模型来存储其属性,例如下面的MyEquipment Each type of equipment will also have a 'category', which would be useful to store as an attribute. 每种类型的设备还将有一个“类别”,将其存储为属性非常有用。

class Category(models.Model):
    code = models.CharField('Category', max_length=4, unique=True)
    description = models.CharField('Description', max_length=30)
    ...

class MyEquipment(models.Model):
    serial = models.IntegerField()
    ...

To save this attribute to my model I could use a foreign key to Category but I don't need to because every record in MyEquipment must be the same Category . 要将这个属性保存到我的模型中,我可以对Category使用外键,但是我不需要,因为MyEquipment每个记录MyEquipment必须是相同的Category So then I thought maybe I could hardcode the Category in the MyEquipment meta like this: 所以我想也许我可以像这样在MyEquipment元数据中对Category进行硬编码:

class MyEquipment(models.Model):
    serial = models.IntegerField()
    ...

    class Meta:
        category = Category.objects.get(code='EC')

But then this would rely on the Category model being populated with data to build the MyEquipment model. 但这将依赖于用数据填充的Category模型来构建MyEquipment模型。 To me this doesn't seem best practice, using data that may or may not exist to define the structure of another model. 对我来说,这似乎不是最佳实践,它使用可能存在或可能不存在的数据来定义另一个模型的结构。 Is there a better way I should be using to set which Category the MyEquipment model is related to? 我应该使用一种更好的方法来设置MyEquipment模型与哪个Category相关吗?

EDIT 编辑

Thanks for the discussion below, it's made me realise perhaps I wasn't clear on my original post. 感谢下面的讨论,这让我意识到也许我对自己的原始帖子不清楚。 So what I want to do is have a way of linking MyEquipment to a Category . 因此,我想做的就是将MyEquipment链接到Category So I can do something like this: 所以我可以做这样的事情:

>>> from myapp.models import MyEquipment
>>> MyEquipment.CATEGORY
<Category: EC>

I want to link the whole model to a Category , so I can process each model in different ways in my view depending on which category it is. 我想将整个模型链接到Category ,因此我可以根据视图的类别以不同的方式处理每个模型。 Having thought about the problem a bit more, I can get this functionality by writing MyEquipment like this: 考虑了更多问题后,我可以通过编写MyEquipment来获得此功能,如下所示:

class MyEquipment(models.Model):
    CATEGORY = Category.objects.get(code='EC')

    serial = models.IntegerField()
    ...

This way works, but is it the best way? 这种方法有效,但这是最好的方法吗? I guess the model would do this get operation everytime the class is instantiated? 我想每次实例化该类时,模型都会执行此操作? Is there a more efficient method? 有没有更有效的方法?

You can't do this anyway; 无论如何,您无法执行此操作; the Meta class doesn't support arbitrary attributes. Meta类不支持任意属性。

The best thing would be to define this as a property, which you can access via the instance itself. 最好的办法是将其定义为属性,您可以通过实例本身访问该属性。 To make it more efficient, you could memoize it on the class. 为了提高效率,您可以在课堂上记住它。

@property
def category(self):
    _category = getattr(self, '_category', None)
    if not _category:
        self.__class__._category = _category = Category.objects.get(code='EC')
    return _category

but ... every record in MyEquipment must be the same Category

Then you don't need any relationship. 然后,您不需要任何关系。 As you said already, every record in MyEquipment are same Category, why do you want to store relation in db? 正如您已经说过的,MyEquipment中的每个记录都是相同的Category,为什么要在db中存储关系?

UPD: Solution with model inheritance UPD:具有模型继承的解决方案

class Place(models.Model):
    category = models.ForeignKey(Category)
    class Meta:
         abstract = True

    def save(self, *args, **kwargs):
        self.category = Category.objects.get(name=self.CATEGORY)
        return super(Place, self).save(*args, **kwargs)

class Restaurant(Place):
    ...fields...
    CATEGORY = 'RE'

class Building(Place):
    ...fields...
    CATEGORY = 'BU'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM