Использую
DRF. Есть модель поста и модель юзера (кастомная). В посте находится 4 поля, связанные с юзером -
ForeignKey и
ManyToManyField. Если создавать пост через через код или через страницу
DRF, все работает нормально. Но как только я пытаюсь создать пост в админке, сразу падает ошибка. Как ее исправить?
Модель поста:
class Post(models.Model):
"""Model of post (article)"""
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
title = models.CharField(max_length=250)
content = models.TextField()
date_created = models.DateTimeField(auto_now_add=True)
views = models.IntegerField(default=0)
rating = models.IntegerField(default=0)
dropped_rating_users = models.ManyToManyField(User, blank=True, related_name='dropped_posts', verbose_name='users who dropped the rating')
raised_rating_users = models.ManyToManyField(User, blank=True, related_name='raised_posts', verbose_name='users who raised the rating')
bookmark_users = models.ManyToManyField(User, blank=True, related_name='bookmarked_posts', verbose_name='users who bookmarked the post')
class Meta:
ordering = ['-date_created']
def __str__(self):
return self.title
Юзер:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
class UserManager(BaseUserManager):
"""Manager for User model"""
def create_user(self, username, email, password):
if not email:
raise ValueError('User must have an email address')
if not username:
raise ValueError('User must have a username')
user = self.model(username = username, email = self.normalize_email(email))
user.set_password(password)
user.save(using = self._db)
return user
def create_superuser(self, username, email, password):
user = self.create_user(username = username, email = email, password = password)
user.is_staff = True
user.is_admin = True
user.is_superuser = True
user.save(using = self._db)
return user
class User(AbstractBaseUser):
"""Model of user"""
username = models.CharField(max_length = 30, unique = True)
email = models.EmailField(max_length = 60, unique = True)
first_name = models.CharField(max_length = 30, null = True, blank = True)
last_name = models.CharField(max_length = 30, null = True, blank = True)
date_joined = models.DateField(auto_now_add = True)
age = models.IntegerField(null = True, blank = True)
is_staff = models.BooleanField(default = False)
is_admin = models.BooleanField(default = False)
is_superuser = models.BooleanField(default = False)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class Meta:
ordering = ['-date_joined']
def __str__(self):
return self.username
def has_perm(self, perm, obj = None):
return self.is_admin
def has_module_perms(self, app_label):
return True
Вот сама ошибка:
IntegrityError at /admin/posts/post/add/
FOREIGN KEY constraint failed