Ashutosh Mishra
Ashutosh Mishra

Reputation: 31

Identity error at attemt_myuser.email.UNIQUE constraint failed in django project

enter code here

#This is my models .py : 'like so'

while post operation through POSTMAN im getting error.

:connection error after that again hitting POST button data is getting and access token is creating but it is showing unique constrain failed.

from django.db import models

from django.contrib.auth.models import (
    BaseUserManager, AbstractBaseUser
)

import datetime

class MyUserManager(BaseUserManager):

    def create_user(self, email, date_of_birth, password=None):
        """
        Creates and saves a User with the given email, date of
        birth and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
            date_of_birth=date_of_birth,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

def create_superuser(self, email, date_of_birth, password=None):
    """
    Creates and saves a superuser with the given email, date of
    birth and password.
    """
    user = self.create_user(
        email,
        password=password,
        date_of_birth=date_of_birth,
    )
    user.is_admin = True
    user.save(using=self._db)
    return user

class MyUser(AbstractBaseUser):

email = models.EmailField(
    verbose_name='email address',
    max_length=255,
    unique=True,

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

objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['date_of_birth']

def __str__(self):
    return self.email

def has_perm(self, perm, obj=None):
    "Does the user have a specific permission?"
    # Simplest possible answer: Yes, always
    return True

def has_module_perms(self, app_label):
    "Does the user have permissions to view the app `app_label`?"
    # Simplest possible answer: Yes, always
    return True

@property
def is_staff(self):
    "Is the user a member of staff?"
    # Simplest possible answer: All admins are staff
    return self.is_admin

class Order(models.Model):

Order_id=models.IntegerField(max_length=20)
Product_Name=models.CharField(max_length=20)
Material_Used=models.CharField(max_length=100)
Texture=models.CharField(max_length=50)
Size=models.CharField(max_length=50)
order_date_time=models.DateField(datetime.datetime.now())

Upvotes: 0

Views: 40

Answers (1)

bmons
bmons

Reputation: 3392

change the code order_date_time=models.DateField(datetime.datetime.now()) to order_date_time=models.DateField(timezone.now) default=datetime.datetime.now() is evaluated at parsing/compile time of the model. It is not changed afterwards. To evaluate now() at the time of adding/updating an object, you have to use: default=timezone.now, which sets now as the callable.

Upvotes: 1

Related Questions