DJANGO - 选择其中一个类别的表格字段。属性?

时间:2016-07-23 21:00:44

标签: django forms models backend

我有两个类:主题(属性"用户名");和用户(具有属性" username1"," username2"," username3")。

我希望此人可以从包含User.username1,User.username2,User.username3的列表中选择Topics.username。

示例:

1-此人输入User.username1,User.username2,User.username3

2-这个人去写一个主题。

3-为此,他需要在3个用户名之间进行选择以发布它。

非常感谢您的帮助

编辑:

#UserProfile Models
class UpperCaseCharField(models.CharField):
def __init__(self, *args, **kwargs):
    super(UpperCaseCharField, self).__init__(*args, **kwargs)
def pre_save(self, model_instance, add):
    value = getattr(model_instance, self.attname, None)
    if value:
        value = value.upper()
        setattr(model_instance, self.attname, value)
        return value
    else:
        return super(UpperCaseCharField, self).pre_save(model_instance, add)

class UserProfile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL, verbose_name=_("profile"), related_name='st')
    username1 = UpperCaseCharField(validators=[RegexValidator(regex='^.{4}$', message='Length has to be 4', code='nomatch')],
            max_length=4, blank=True, unique=True)
    username2 = UpperCaseCharField(validators=[RegexValidator(regex='^.{4}$', message='Length has to be 4', code='nomatch')],
            max_length=4, blank=True, unique=True)
    username3 = UpperCaseCharField(validators=[RegexValidator(regex='^.{4}$', message='Length has to be 4', code='nomatch')],
            max_length=4, blank=True, unique=True)

我的主题模型没有做太多,所以我觉得它有点不必要。很抱歉延迟回答

0 个答案:

没有答案