如何重写Django中的model.Manager.create()方法?

2024-05-19 20:26:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我有很多Hardware模型,它们都有一个具有各种特性的HardwareType。就像这样:

# models.py
from django.db import models

class HardwareType(model.Models):
    name = models.CharField(max_length=32, unique=True)

    # some characteristics of this particular piece of hardware
    weight = models.DecimalField(max_digits=12, decimal_places=3)
    # and more [...]        

class Hardware(models.Model):

    type = models.ForeignKey(HardwareType)

    # some attributes
    is_installed = models.BooleanField()
    location_installed = models.TextField()
    # and more [...]

如果我想添加一个新的Hardware对象,我首先必须每次都检索HardwareType,这不是很干燥:

tmp_hd_type = HardwareType.objects.get(name='NG35001')
new_hd = Hardware.objects.create(type=tmp_hd_type, is_installed=True, ...)

因此,我试图重写HardwareManager.create()方法,以便在创建新的Hardware时自动导入类型,如下所示:

# models.py
from django.db import models

class HardwareType(model.Models):
    name = models.CharField(max_length=32, unique=True)

    # some characteristics of this particular piece of hardware
    weight = models.DecimalField(max_digits=12, decimal_places=3)
    # and more [...] 

class HardwareManager(models.Manager):
    def create(self, *args, **kwargs):
        if 'type' in kwargs and kwargs['type'] is str:
            kwargs['type'] = HardwareType.objects.get(name=kwargs['type'])
        super(HardwareManager, self).create(*args, **kwargs)       

class Hardware(models.Model):
    objects = HardwareManager()

    type = models.ForeignKey(HardwareType)

    # some attributes
    is_installed = models.BooleanField()
    location_installed = models.TextField()
    # and more [...]

# so then I should be able to do:
new_hd = Hardware.objects.create(type='ND35001', is_installed=True, ...)

但是我不断地从ORM中得到错误和非常奇怪的行为(这里没有,但是如果需要的话我可以发布它们)。我在Django文档和SO线程中搜索过,但大多数情况下,我最终找到的解决方案是:

  • Hardware.save()方法被重写(我应该在那里得到HardwareType?)或者
  • 管理器定义了一个新的create_something方法,该方法调用self.create()

我也开始深入研究代码,发现Manager是某种特殊的QuerySet类型,但我不知道如何从那里继续。我真的很想把create方法替换到适当的位置,但我似乎无法处理这个问题。是什么阻止我做我想做的事?


Tags: installedand方法nametrueobjectsismodels
2条回答

Alasdair的答案中得到的信息有助于捕获字符串和unicode字符串,但实际上缺少的是在调用HardwareManager.create()方法中的super(HardwareManager, self).create(*args, **kwargs)之前的return语句。

昨天晚上我在测试中遇到的错误(当编码不是一个好主意时很累:p)是ValueError: Cannot assign None: [...] does not allow null values.,因为我拥有的create()d的后续用法是None,因为我的create()方法没有return。真是个愚蠢的错误!

最终修正代码:

class HardwareManager(models.Manager):
    def create(self, *args, **kwargs):
        if 'type' in kwargs and isinstance(kwargs['type'], basestring):
            kwargs['type'] = HardwareType.objects.get(name=kwargs['type'])
        return super(HardwareManager, self).create(*args, **kwargs)   

没有看到回溯,我想问题出在这条线上。

    if 'type' in kwargs and kwargs['type'] is str:

这是检查kwargs['type']是否与str是同一个对象,该对象始终为false。

在Python 3中,要检查“kwargs['type']是否为字符串,应执行以下操作:

    if 'type' in kwargs and isinstance(kwargs['type'], str):

如果您使用的是Python 2,那么应该使用basestring,来捕获字节字符串和unicode字符串。

    if 'type' in kwargs and isinstance(kwargs['type'], basestring):

相关问题 更多 >