【问题标题】:How to insert data into 1-to-1 relationship tables in django如何在django中将数据插入到1对1关系表中
【发布时间】:2013-02-02 01:34:37
【问题描述】:

我是 django 的新手。我使用 django User 和 2 个扩展表(profile_table、more_table),每个表都与 User 具有一对一的关系。所以在models.py下,我有2个类(一个用于profile_table,另一个用于more_table)。当我创建一个新用户时,我会将基本数据插入到 auth_user 表中,并将其他信息插入到 profile_talbe 和 more_table 中。如何以及在何处构建此插入方法?我应该使用班级管理器吗?或覆盖保存功能?或者在 profile_table 类下创建一个类方法?

谢谢

【问题讨论】:

    标签: django model


    【解决方案1】:

    如果您从配置文件模型创建新实例,您应该在配置文件模型类中编写静态方法。 示例:

    class Profile(models.Model):
       name = models.CharField(max_length=100)
    
       @staticmethod
       def create_profile(name=name):
            profile = Profile(name=name)
            profile.save()
    
            return profile
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多