如何以正确的方式创建ModelCopy(django)

时间:2014-07-29 07:02:24

标签: python django model

我有一个简历模型

class Resume(models.Model):
    owner = models.ForeignKey(Member)
    realname = models.CharField(max_length=30)
    sex = models.IntegerField(blank=False, choices=SEX_CHOICES, default=1)
    education = models.IntegerField(blank=False, choices=EDUCATION_CHOICES)

    expierence = models.IntegerField(blank=False, choices=EXPERIENCE_CHOICES)
    expect_post = models.IntegerField(blank=False, choices=POST_CHOICES)
    expect_salary = models.IntegerField(blank=False, choices=SALARY_CHOICES)
    city = models.ForeignKey(City)
    location = models.ForeignKey(Location, null=True)
    .....

现在我想将一份简历发送给招聘人员(要求是:招聘人员看不到更新,所以我必须这样做。)

所以我这样做:

class ResumeCopy(Resume):
    def copy(self, resume):
        for f in self._meta.fields:
            setattr(self, f.name, getattr(resume, f.name))

但它会以这种方式隐式创建名为OneToOneField的Not NUll resume_ptr。 我不需要这个领域,有没有更好的方法来达到我的目的?

1 个答案:

答案 0 :(得分:1)

创建一个抽象模型,它是ResumeResumeCopy的基础。

class BaseResume(models.Model):
    class Meta:
        abstract = True

    owner = models.ForeignKey(Member)
    realname = models.CharField(max_length=30)
    sex = models.IntegerField(blank=False, choices=SEX_CHOICES, default=1)
    education = models.IntegerField(blank=False, choices=EDUCATION_CHOICES)

    expierence = models.IntegerField(blank=False, choices=EXPERIENCE_CHOICES)
    expect_post = models.IntegerField(blank=False, choices=POST_CHOICES)
    expect_salary = models.IntegerField(blank=False, choices=SALARY_CHOICES)
    city = models.ForeignKey(City)
    location = models.ForeignKey(Location, null=True)

class Resume(BaseResume):
    pass

class ResumeCopy(BaseResume):
    pass