我有两个用户模型,如何将它们组合在一起? (AbstractBaseUser)

时间:2019-10-02 19:26:08

标签: authentication django-models

我正在使用 User 模型创建网站,但是遇到了问题。对于User模型在Django中的工作方式,我感到非常困惑。

在我的管理站点中,有两个用户,一个在“身份验证和授权”下,另一个在“市场”下。超级用户进入身份验证一。

这是我当前的代码:

# market/models.py

from django.db import models
from datetime import date
from PIL import Image
from django.urls import reverse
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
import uuid # Required for unique id

from django.core.validators import MaxValueValidator, MinValueValidator

class **User**(AbstractBaseUser):
    username = models.CharField(primary_key=True, max_length=30, help_text='Enter a unique username.', blank=False)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    date_of_birth = models.DateField(null=True, blank=True)
    email = models.EmailField(max_length=255, unique=True)
    phone = models.CharField(max_length=20)
    address = models.CharField(max_length=255)
    state = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    zip_code = models.CharField(max_length=10)
    payment_method = models.CharField(max_length=16, default='None')
    order_seller = models.CharField(max_length=100, default='None')
    order_buyer = models.CharField(max_length=100, default='None')

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    USERNAME_FIELD = 'username'
    #REQUIRED_FIELDS = []

    # Meta
    class Meta:
        ordering = ['first_name', 'last_name']

    # Method
    def __str__(self):
        return f'{self.username}'

    def get_absolute_url(self):
        """Returns the url to access a particular instances of the model. """
        return reverse('user-detail', args=[str(self.username)])



class Order(models.Model):
    # id = models.AutoField(primary_key=True)
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, help_text='Unique order number')

    # time and date the order was submitted
    date_ordered = models.DateTimeField(auto_now_add = True)

    # ForeignKey of the buyer
    buyer = models.ForeignKey(**User**, on_delete=models.CASCADE, null=True)

0 个答案:

没有答案