如何组合表?

时间:2014-04-20 09:56:56

标签: python django

请帮助您动态选择状态。

我有一个模特:

class UserProfile(models.Model):
    CHOICES_status = (
        ('0', 'none'),
        ('1', 'status1'),
        ('2', 'status2'),
        ('3', 'status3'),
        ('4', 'status4'),
    )   

    user = models.OneToOneField(User)
    family = models.CharField(max_length=30, blank=True)
    name = models.CharField(max_length=30, blank=True)
    nation = models.CharField(max_length=30)
    status = models.CharField(max_length=30, blank=True, choices=CHOICES_status, default='0')
    avatar = models.ImageField(upload_to='userprofile/', blank=True)
    nation_show = models.BooleanField(default=True, blank=True)

它有一组固定的状态。创建用户选择某个状态。

我将一组状态放在一个单独的表中:

class UserStatus(models.Model):
    status = models.CharField(max_length=30)

现在我需要根据UserStatus模型动态生成变量CHOICES_status

1 个答案:

答案 0 :(得分:0)

#models.py
class ProfileStatus(models.Model):
    status = models.CharField(max_length=32)


class Profile(models.Model):
    name = models.CharField(max_length=32)
    status = models.ForeignKey(ProfileStatus)


#forms.py
class ProfileForm(forms.Form):
    name = forms.CharField(max_length=32)
    status = forms.ModelChoiceField(queryset=ProfileStatus.objects.all())