简体   繁体   English

使用UserCreationForm模型用户的外键

[英]Using foreign key of the UserCreationForm model User

I have made a signup page using built in UserCreationForm of django. 我使用django的内置UserCreationForm制作了一个注册页面。

signup.html 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

But I also need to make other tables in models.py . 但是我还需要在models.py创建其他表。 So if in another table category I need to make a foreign key of the primary key of this built in User of UserCreationForm . 因此,如果在另一个表category我需要使用UserCreationForm内置User中的主键作为外键。 What is the primary key in this? 什么是主键?

models.py 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"

What do I write in place of # ?? 我要写些什么来代替#??

Just point to the User model: 只需指向User模型:

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

or better, in case you might want to customise the User model: 或更佳的选择,以防您可能需要自定义用户模型:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM