我正在使用一个自定义用户模型,当我试图在其他模型中引用它时,我得到了一个错误。你知道吗
Exception Value:
NOT NULL constraint failed: send_appdata.product_id_id
视图.py
from django.contrib.auth import get_user_model
AppOnboarding = get_user_model()
# get data from json
@api_view(['POST'])
def index(request):
product_id = AppOnboarding.objects.get(pk=request.data['product_id'])
product_name = request.data['product_name']
product_url = request.data['product_url']
subject = request.data['subject']
body = request.data['body']
recipient_list = request.data['recipient_list']
sender_mail = request.data['sender_mail']
email_type = request.data['email_type']
broadcast_mail = request.data['broadcast_mail']
email_status = status.HTTP_200_OK
location = request.data['location']
# make an object of AppData
app_data = AppData(product_id=product_id, product_name=product_name, product_url=product_url,
subject=subject, body=body, recipient_list=recipient_list, sender_mail=sender_mail, email_type=email_type, broadcast_mail=broadcast_mail, email_status=email_status, location=location)
# save it to DB
app_data.save()
型号.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.contrib.auth import get_user_model
AppOnboarding = get_user_model()
class AppData(models.Model):
# app_data_id = models.IntegerField(primary_key=True)
product_id = models.ForeignKey(AppOnboarding, on_delete=models.PROTECT)
product_name = models.CharField(max_length=100)
product_url = models.URLField()
subject = models.CharField(max_length=100)
body = models.TextField()
recipient_list = models.TextField()
sender_mail = models.EmailField(max_length=254)
email_type = models.CharField(max_length=50)
broadcast_mail = models.BooleanField()
email_status = models.CharField(max_length=50)
date_time = models.DateTimeField(auto_now_add=True)
location = models.CharField(max_length=100)
AppOnboarding是另一个应用程序中的自定义用户模型。你知道吗
我探讨了类似的问题,但未能解决问题。 非常感谢您的帮助。你知道吗
应该是
最近添加了这个外键
product_id
(在其他模型字段之后)? 如果是,则需要使其可为null(null=True)并运行makemigrations
,然后migrate
当我用python shell手动检查时发现了错误,Apponboarding.objects.get获取(id=1)返回时不退出,但Apponboarding.objects.get获取(id=2)和其他,它返回的是电子邮件地址而不是id。仍然不清楚id是如何从2而不是1开始的(因为它是auto字段)以及为什么返回电子邮件而不是id
相关问题 更多 >
编程相关推荐