Python 如何在 Django 中默认 IntegerField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22892651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:54:24  来源:igfitidea点击:

How to default IntegerField in Django

pythondjango

提问by BasiL

I was studying some Django, and came across a small problem.

我正在学习一些 Django,并遇到了一个小问题。

I am looking for a way to make suit_length equal to customer's height by default. This kind of code works for me:

我正在寻找一种默认情况下使suit_length 等于客户身高的方法。这种代码对我有用:

    class Customer(models.Model):
        name = models.CharField(max_length=20)
        height = models.IntegerField(default=170)

    class Suit(models.Model)
        customer = models.ForeignKey(Customer)
        suit_design = models.CharField(max_length=100)
        suit_length = models.IntegerField(default=0)
        def get_height(self):
            self.suit_length = self.customer.height
            return

But every time I create a new Suit its default suit_length = 0, and I have to run get_height() to get what I want. Is there a way to default suit_length to customer.height and to avoid running get_height() every time I create a new Suit? I am probably looking for smth like this:

但是每次我创建一个新的套装时,它的默认套装长度 = 0,我必须运行 get_height() 才能得到我想要的。有没有办法将suit_length默认为customer.height并避免每次创建新西装时都运行get_height()?我可能正在寻找这样的东西:

    class Customer(models.Model):
        name = models.CharField(max_length=20)
        height = models.IntegerField(default=170)

    class Suit(models.Model)
        customer = models.ForeignKey(Customer)
        suit_design = models.CharField(max_length=500)
        suit_length = models.IntegerField(default=lambda:self.customer.height)

But this code doesn't work.

但是这段代码不起作用。

回答by paulus

Django uses the save() method to actually save the data. You may overwrite it like so:

Django 使用 save() 方法来实际保存数据。你可以像这样覆盖它:

class Suit(models.Model):
    def save(self):
        if self.suit_length == 0:
            self.suit_length = self.customer.height
        super(Suit, self).save()

It is not overwriting the default, but it achieves your goal.

它不会覆盖默认值,但可以实现您的目标。

回答by Bill Torcaso

You want an invariant to be true at run-time, which is that the initial length of a Suit is equal to the height of a Customer.

您希望不变量在运行时为真,即西装的初始长度等于客户的高度。

Doesn't the pythonic way to do this involve __ init__() ?

这样做的pythonic方法不涉及 __ init__() 吗?

    class Suit(models.Model)
        customer = models.ForeignKey(Customer)
        suit_design = models.CharField(max_length=500)
        suit_length = models.IntegerField(default=lambda:self.customer.height)

        def __init__(self, args, kwargs):
            super(models.Model, args, kwargs)
            if kwargs['customer']:
                self.customer = kwargs['customer']
                # this creates the invariant
                self.suit_length = self.customer.height  
            else:
                raise ValueError("Must have a customer before constructing a Suit")
            # More code for suit_design, etc.
        }