V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
anthoy
V2EX  ›  Python

Django rest framework 中的 serializers 中的 manytomany 问题

  •  
  •   anthoy · 2018-08-31 10:50:31 +08:00 · 1474 次点击
    这是一个创建于 2058 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Django rest framework 中的 serializers 的代码如下:

    class AirshipDeviceCreateSerializer(serializers.ModelSerializer):
        # 获取当前登录的用户,HiddenField 不采用用户的输入而使用默认
        # user 是多对多的外键
        user = serializers.HiddenField(
             default=serializers.CurrentUserDefault()
        )
    
        class Meta:
            model = AirshipDevice
            fields = ('user', 'name', 'type', 'device_id')
    

    其中 user 相应的多对多的外键,我想直接获取当前用户进行保存,不用输入,但这样写会提示 user model is not iterable,我猜想多对多应该不是这样写,但我没有思路不知道怎么写?问一下大家有没有经验提示一下?

    我尝试过使用 ModelSerializer 的 Listfield,不知道是不是我写的不正确,使用失败

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   874 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 21:49 · PVG 05:49 · LAX 14:49 · JFK 17:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.