简体   繁体   中英

Using foreign key of the UserCreationForm model User

I have made a signup page using built in UserCreationForm of django.

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 . So if in another table category I need to make a foreign key of the primary key of this built in User of UserCreationForm . What is the primary key in this?

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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