简体   繁体   English

Django - 自定义 model 保存方法不显示属性值

[英]Django - custom model save method doesn't show attributes values

while trying to save an instance of a model, I can't access the attributes values in the custom save() method.在尝试保存 model 的实例时,我无法访问自定义 save() 方法中的属性值。 I think it should be possible straight forward, just like the docs state.我认为这应该是可能的,就像文档state 一样。 Here's what my code looks like:这是我的代码的样子:

class ModelName(models.Model):
    attribute1 = models.ManyToManyField("app.ModelName2", related_name="model_name_2")
    attribute2 = models.ForeignKey("app.ModelName3", related_name="model_name_3")

    def save(self, *args, **kwargs):
        super().save()
        print(self.attribute1.all()) # <---- it prints an empty qs, but in reality there are instances passed to attribute1 field.

Does anyone have any idea why that would happen?有谁知道为什么会发生这种情况? I'm pretty sure I'm missing something super obvious.我很确定我错过了一些非常明显的东西。 Thanks in advance!提前致谢!

Which App Inside Create This Model,You Have To Install App In setting.py哪个应用里面创建这个Model,你必须在setting.py中安装应用

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '<your_app_name>'
]
AUTH_USER_MODEL = '<app_name>.<model_name>

' '

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

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