关系数据库模型中的Django POST方法

时间:2017-06-10 07:00:49

标签: python django python-3.x django-models django-rest-framework

我想在url中为已定义的用户ID创建Object2。下面显示的示例中的GET方法工作正常,但我无法创建POST方法。我将不胜感激。假设我在数据库中有免费的样本对象:

User:
id

Object1:
id
user_id

Object2:
id
object1_id

urls.py中,它以这种方式显示:

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

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')

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)

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__'

我正在尝试这种方式,但我在对象object3 = models.ForeignKey(Object3, related_name='object2')中遇到class Object2(models.Model):的问题。也就是说,当我发送GET时,我得到了响应,例如:

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

但在POST中我想以这种方式只发送object3的id:

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

然而此刻我得到了一个奇怪的错误:

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

但在我看来,帮助我会很困难。我不知道它为什么要寻找i表。我没有任何定义它。我甚至没有名称为i的对象。

总结一下,我想只改变POST。 GET是正确的。有什么建议吗?

完整堆栈跟踪:

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 个答案:

答案 0 :(得分:1)

根据@OP的要求重写我的答案。

这是一个例子,

@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)

这只是一个例子,我没有尝试或测试过。我写这篇文章只是为了了解你的案例。所以,如果有任何错误,请原谅我。

修改

我建议为这两种方法编写单独的序列化程序。

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

在序列化程序中为read_only=True设置字段选项object3,然后该字段将不包含在请求的post方法中。

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