简体   繁体   English

Django Model Inheritance - 实例属性设置不正确

[英]Django Model Inheritance - Instance attributes not set properly

Alas, my code was working perfect until I decided to "fix" it using inheritance for linked classes.唉,我的代码运行完美,直到我决定使用 inheritance 链接类来“修复”它。

I have a UserProgram class that inherits from Program and MyUser .我有一个继承自ProgramUserProgramMyUser When I changed the class definition to UserProgram(Program, MyUser) , I started getting strange behavior with my UserProgram.objects.get() call, which gets its data from a stored procedure via get_data_pk() :当我将 class 定义更改为UserProgram(Program, MyUser)时,我的UserProgram.objects.get()调用开始出现奇怪的行为,该调用通过get_data_pk()从存储过程中获取数据:

raw('select 1 as id, * from SP_GetUserProgram(%s,%s,%s)', params)

在此处输入图像描述

After adding the inheritance, I see the new objects have all the inherited fields, but the attributes are not being assigned the correct values now.添加 inheritance 后,我看到新对象具有所有继承的字段,但现在没有为属性分配正确的值。 For instance, maxbudget and availablebudget should both be 400000 , but that is incorrectly assigned to uploaddirectoryid and is_active .例如, maxbudgetavailablebudget都应该是400000 ,但它被错误地分配给了uploaddirectoryidis_active

在此处输入图像描述

Creating the UserProgram objects manually via UserProgram(...) works fine, but not via the DB call anymore.通过UserProgram(...)手动创建UserProgram对象可以正常工作,但不再通过 DB 调用。 I'm not sure what's going on.我不确定发生了什么事。 Any ideas?有任何想法吗? Here's a simplified version of my code.这是我的代码的简化版本。

models.py模型.py

class UserProgramManager(models.Manager):
    def get(self, userid, programname, schoolyear):
        return get_data_pk(self, 
          'SP_GetUserProgram(%s,%s,%s)', (userid, programname, schoolyear)
        )

class MyModel(models.Model):
    class Meta:
        managed = False
        abstract = True

class MyUser(AbstractBaseUser):
    userid = models.IntegerField(primary_key=True)
    objects = MyUserManager()

class Program(MyModel):
    programname = models.CharField(primary_key=True)
    schoolyear = models.SmallIntegerField()

    def __init__(self,schoolyear=None,programname=None,*args,**kwargs):
        super(Program, self).__init__(*args,**kwargs)

        self.schoolyear = schoolyear
        self.programname = programname

        # Do some extra processing (not included here)

class UserProgram(Program, MyUser):
    uploaddirectoryid = models.CharField(primary_key=True)
    objects = UserProgramManager()

test.py测试.py

# Works fine
myuserprogram = UserProgram(
  userid=17,
  schoolyear=2020,
  programname='incentive'
)

# Attributes are not set correctly
myuserprogram = UserProgram.objects.get(
  userid=17,
  schoolyear=2020,
  programname='incentive'
)

In the words of Homer Simpson: I am so smart.用荷马辛普森的话来说:我很聪明。 S...M...R...T. S...M...R...T。

It looks like my __init__ functions were causing the problems.看起来我的__init__函数导致了问题。 I removed the named parameters and just left *args, **kwargs and it works fine:我删除了命名参数,只留下了*args, **kwargs ,它工作正常:

def __init__(self,*args,**kwargs):
  super(Program, self).__init__(*args,**kwargs)

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

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