Django - POST method in relational database model

2019-05-27 10:12发布

问题:

I would like to create Object2 for defined user ID in url. GET method in the example shown below works fine, but I can't create POST method. I will be grateful for help. Let's assume I have free sample objects in database:

User:
id

Object1:
id
user_id

Object2:
id
object1_id

In urls.py it looks in this way:

url(r'^users/(?P<user_id>[0-9]+)/object2$', views.UserObject2),

In models.py:

from django.contrib.auth.models import User

class Object1(models.Model):

    user = models.ForeignKey(User)

class Object2(models.Model):

    object1 = models.ForeignKey(Object1)
    object3 = models.ForeignKey(Object3, related_name='object2')

In views.py:

@api_view(['GET', 'POST']) 
def UserObject2(request, user_id): 
    user = User.objects.get(id=user_id)
    if request.method == 'POST':
        serializer = Object2Serializer(data=request.data)
        object1 = Object1.objects.get(user=user)
        if serializer.is_valid():
            serializer.save(object1=object1) 
        else:
            return Response(serializer.errors)
    else:
        object2 = Object2.objects.filter(object1__user=user)
        serializer = Object2Serializer(object2, many=True)
    return Response(serializer.data)

In serializers.py:

from django.contrib.auth.models import User

class Object3Serializer(serializers.ModelSerializer):

    class Meta:
        model = Object3
        depth = 1
        fields = '__all__'

class UserSerializer(serializers.ModelSerializer):

    def create(self, validated_data):
      user = User.objects.create_user(**validated_data)
      return user

    class Meta:
        model = User

        fields = '__all__'

class Object2Serializer(serializers.ModelSerializer):
    object3 = Object3Serializer(read_only=True)
    class Meta:
        model = Object2

        fields = ('object3', 'number', 'date')

class Object1Serializer(serializers.ModelSerializer):

    class Meta:
        model = Object1

        fields = '__all__'

I'm trying in this way, but I have problem with object3 = models.ForeignKey(Object3, related_name='object2') in Object class Object2(models.Model): To wit, when I send GET I get in response for example:

[
  {
    "id": 14,
    "object3": {
      "sth1": 4,
      "sth2": "response",
      "sth3": "response1",
    },
    "number": 123,
    "date": "2017-06-07",
  }
]

But in POST I would like to send only id of object3 in this way:

  {
    "object3": 3,
    "number": 123,
    "date": "2017-06-07"
  }

However at the moment I get strange error:

(1146, "Table 'name_of_my_table.i' doesn't exist")

But it seems to me that it will be difficult to help me. I have no idea why it's looking for i table. I do not have anywhere defined it. I don't have even object with name i.

To sum up, I would like to change only POST. GET is correct. Any suggestions?

Full stack trace:

Internal Server Error: /users/3/object2
Traceback (most recent call last):
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/connections.py", line 292, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1146, "Table 'name_of_my_table.i' doesn't exist")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/rest_framework/views.py", line 489, in dispatch
    response = self.handle_exception(exc)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/rest_framework/views.py", line 449, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/rest_framework/views.py", line 486, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/rest_framework/decorators.py", line 52, in handler
    return func(*args, **kwargs)
  File "/Users/myUser/Projects/myProject/Folder/backend/folder/companies/views.py", line 442, in UserObject23
    serializer.save(object1=object1)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/rest_framework/serializers.py", line 215, in save
    self.instance = self.create(validated_data)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/rest_framework/serializers.py", line 916, in create
    instance = ModelClass.objects.create(**validated_data)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/query.py", line 393, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/base.py", line 922, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/base.py", line 961, in _do_insert
    using=using, raw=raw)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/query.py", line 1060, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 101, in execute
    return self.cursor.execute(query, args)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/Users/myUser/Projects/myProject/lib/python3.6/site-packages/MySQLdb/connections.py", line 292, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'name_of_my_table.i' doesn't exist")
[10/Jun/2017 09:45:32] "POST /users/3/object2 HTTP/1.1" 500 20503

回答1:

First of all, run python manage.py makemigrations and then, python manage.py migrate.

serializers.py

class Object2GetSerializer(serializers.ModelSerializer):
    object3 = Object3Serializer(read_only=True) 
    class Meta: 
        model = Object2 
        fields = ('object3', 'other_fields')

class Object2PostSerializer(serializers.ModelSerializer): 
    class Meta: 
        model = Object2 
        fields = ('object3', 'object1', 'number', 'date')

Views.py

@api_view(['GET', 'POST']) 
def UserObject2(request, user_id): 
    user= User.objects.get(id=user_id)
    if request.method == 'POST':
        object1 = Object1.objects.get(user=user)
        serializer = Object2PostSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save(object1=object1) 
        else:
            return Response(serializer.errors)
    else:
        object2 = Object2.objects.filter(object1__user=user)
        serializer = Object2GetSerializer(object2, many=True)
    return Response(serializer.data)