我使用的是自定义用户模型,当我尝试在其他模型中引用它时,出现错误。
[v24x,v24y]
views.py
Exception Value:
NOT NULL constraint failed: send_appdata.product_id_id
models.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()
AppOnboarding是另一个应用程序中的自定义用户模型。
我探讨了类似的问题,但无法解决问题。 我们非常感谢您的帮助。
答案 0 :(得分:0)
最近(在其余模型字段之后)添加了此外键product_id
吗?
如果是,则需要将其设置为可为空(null = True),然后运行makemigrations
,然后运行migrate
答案 1 :(得分:0)
发现错误,当我使用python shell手动检查时,Apponboarding.objects.get(id = 1)返回不退出但Apponboarding.objects.get(id = 2)和其他返回了电子邮件地址而不是ID。尚不清楚ID如何从2而不是1开始(因为它是自动字段),以及为什么返回电子邮件而不是ID的原因。
答案 2 :(得分:0)
product_id = models.ForeignKey(AppOnboarding, on_delete=models.PROTECT)
应该是
product = models.ForeignKey(AppOnboarding, on_delete=models.PROTECT)