推荐学习书目
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 · Aug 31, 2018 · 1858 views
    This topic created in 2840 days ago, the information mentioned may be changed or developed.

    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,不知道是不是我写的不正确,使用失败

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   882 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:50 · PVG 04:50 · LAX 13:50 · JFK 16:50
    ♥ Do have faith in what you're doing.