如何在模型中创建一个字段只存在于另一个字段上遇到的条件?

时间:2014-03-30 14:02:15

标签: django models

我试图做到这一点,只有当MusicPackState.type等于loan时才有 对正在借给它的会员的提及。我是否采取正确的方式来做这件事?

class Member(models.Model):
    firstName = models.CharField(max_length = 20)
    lastName = models.CharField(max_length = 20)
    address = models.ForeignKey(models.ForeignKey(Address)
    memberContactInfo = models.ForeignKey(MemberContactInfo)

class MusicPack(models.Model)
    title = models.CharField(max_length = 20)
    composer = models.CharField(max_length = 20)
    arranger = models.CharField(max_length = 20)

class MusicPackState(models.Model)
    TYPES_CHOICES = (
        ('ISSUED', 'Issued'),
        ('LIBRARY', 'Library'),
        ('LOAN', 'Loan'),
        ('ENTERTAINMENTFOLDER', 'Entertainment Folder')
    )
    type = models.CharField(_('Type'), max_length = 20, choices = TYPES_CHOICES)


    def if_loan(self)
        if type == 'Loan'
                member = models.ForeignKey(Member)

0 个答案:

没有答案