简体   繁体   English

Django 用户模型外键

[英]Django User model foreign key

I'm struggling writing a relational database in Django.我正在努力在 Django 中编写关系数据库。 Basically what I am trying to achieve is to create a user foreign key to one of the tables.基本上我想要实现的是为其中一个表创建一个用户外键。

I found plenty of similar questions over here but for some reason, it doesn't seem to work on my cause, maybe it's because of the version or I don't know.我在这里发现了很多类似的问题,但由于某种原因,它似乎对我的事业不起作用,也许是因为版本的原因,或者我不知道。

This is my model:这是我的模型:

from django.contrib.auth.models import User


class Category(models.Model):
    name = models.CharField(max_length=255)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

view:看法:

from rest_framework import viewsets
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated

from .models import Category
from .serializers import CategorySerializer


class CategoryView(viewsets.ModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = [IsAuthenticated]
    authentication_classes = [SessionAuthentication, BasicAuthentication]

and for autenthification I used the build-in functionality对于身份验证,我使用了内置功能

path('api-auth/', include('rest_framework.urls')),

and this is the error that I get:这是我得到的错误:

IntegrityError at /category/
NOT NULL constraint failed: category_category.user_id
Request Method: POST
Request URL:    http://127.0.0.1:8000/category/
Django Version: 2.2.5
Exception Type: IntegrityError
Exception Value:    
NOT NULL constraint failed: category_category.user_id
Exception Location: C:\Users\Terkea\Anaconda3\envs\django_api_template\lib\site-packages\django\db\backends\sqlite3\base.py in execute, line 383
Python Executable:  C:\Users\Terkea\Anaconda3\envs\django_api_template\python.exe
Python Version: 3.7.5
Python Path:    
['C:\\Users\\Terkea\\PycharmProjects\\django_api_template',
 'C:\\Users\\Terkea\\PycharmProjects\\django_api_template',
 'C:\\Program Files\\JetBrains\\PyCharm '
 '2019.2.3\\plugins\\python\\helpers\\pycharm_display',
 'C:\\Users\\Terkea\\Anaconda3\\envs\\django_api_template\\python37.zip',
 'C:\\Users\\Terkea\\Anaconda3\\envs\\django_api_template\\DLLs',
 'C:\\Users\\Terkea\\Anaconda3\\envs\\django_api_template\\lib',
 'C:\\Users\\Terkea\\Anaconda3\\envs\\django_api_template',
 'C:\\Users\\Terkea\\Anaconda3\\envs\\django_api_template\\lib\\site-packages',
 'C:\\Program Files\\JetBrains\\PyCharm '
 '2019.2.3\\plugins\\python\\helpers\\pycharm_matplotlib_backend']
Server time:    Wed, 11 Dec 2019 08:08:11 +0000

You can override the perform_create method.您可以覆盖perform_create方法。

class CategoryView(viewsets.ModelViewSet):
    queryset = Category.objects.all()
    serializer_class = CategorySerializer
    permission_classes = [IsAuthenticated]
    authentication_classes = [SessionAuthentication, BasicAuthentication]


    def perform_create(self, serializer):
        serializer.save(user=self.request.user)

from the docs:从文档:

perform_create(self, serializer) - Called by CreateModelMixin when saving a new object instance. perform_create(self, serializer) - 保存新对象实例时由 CreateModelMixin 调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM