简体   繁体   中英

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. I think it should be possible straight forward, just like the docs 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

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>

'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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