I have two models in my database, an 'Account' model (which is a custom user model) and a 'Return' model.
My database worked fine up to the point of adding the 'Return' model, which has a ForeignKey relationship with the 'User' model, which I suspect is causing the problem. (Each return belongs to an existing user. In the admin panel, the option box is populated with existing users so I thought this was working correctly?)
Appreciate any help with this!
Error:
IntegrityError at /admin/returns/return/add/
FOREIGN KEY constraint failed
Request Method: POST Request URL: http://127.0.0.1:8000/admin/returns/return/add/ Django Version: 3.0.3 Exception Type: IntegrityError Exception Value:
FOREIGN KEY constraint failed
Here is my Return app's models.py:
from django.db import models
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.
class Return(models.Model):
user = models.ForeignKey(User, related_name="returns", on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now=True)
last_edited = models.DateTimeField(null=True)
TAX_YEAR_CHOICES = [
('2019', '2019'),
('2020', '2020'),
('2021', '2021'),
]
tax_year_ending = models.CharField(
choices=TAX_YEAR_CHOICES,
max_length=4,
blank=False,
null=False,
)
RETURN_STATUS_CHOICES = [
('1', 'In progress'),
('2', 'Completed, awaiting review'),
('3', 'Completed, under review'),
('4', 'Completed, awaiting client action'),
('5', 'Submitted to HMRC'),
]
return_status = models.CharField(
choices=RETURN_STATUS_CHOICES,
max_length=1,
default=1,
blank=False,
null=False,
)
Here is my custom user class (also setup in settings.py):
class Account(AbstractBaseUser):
email = models.EmailField(verbose_name='email', max_length=60, unique=True) # tells Django this is the unique ID
#following required for the AbstractBaseUser class
date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
is_admin = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
#tell django we want to use email as our username field (for logins)
USERNAME_FIELD = 'email'
#tells Django to user AccountManager
objects = AccountManager()
#tell django which fields are required on registration:
#REQUIRED_FIELDS = []
#what to return when we print the Account object
def __str__(self):
return self.email
#following required for AbstractBaseUser class
#does the user have permissions, yes if is_admin
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
Here is the post request information from the errog log: user
'3'
last_edited_0
'2020-05-03'
last_edited_1
'13:01:23'
tax_year_ending
'2019'
return_status
'3'
_addanother
'Save and add another'
(I have confirmed that there is a user with pk '3')
Thanks again, really appreciate it.
You need to record the instance of user on behalf of pk number:
User.objects.get(pk=3)
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.