使用UserCreationForm模型User的外键

时间:2017-09-20 14:27:20

标签: python django

我使用django的内置UserCreationForm创建了一个注册页面。

signup.html

class UserCreationForm(UserCreationForm):
    email = EmailField(label=_("Email address"), required=True, help_text=_("Required."))

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

但我还需要在models.py中制作其他表格。因此,如果在另一个表category中,我需要创建此内置用户UserCreationForm的主键的外键。这主要是什么?

models.py

class category(models.Model):
    uid = models.ForeignKey(#)
    cname = models.CharField(max_length=20)

    def __unicode__(self):
        return u"{} {}".format(self.uid, self.cname)
    class Meta:
        db_table = "category"

我用什么来代替#??

1 个答案:

答案 0 :(得分:1)

只需指向User型号:

from django.contrib.auth import User
uid = models.ForeignKey(User)

或更好,如果您可能想要自定义用户模型:

from django.conf import settings
uid = models.ForeignKey(settings.AUTH_USER_MODEL)