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
wxiao333
V2EX  ›  Python

drf 里面 onetoonefield 的 ModelSerializer 该怎样写?

  •  
  •   wxiao333 · 2018-11-28 15:56:08 +08:00 · 1485 次点击
    这是一个创建于 1947 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如:
    class Engine(models.Model):
    name = models.CharField(max_length=25)



    class Car(models.Model):
    name = models.CharField(max_length=25)
    engine = models.OneToOneField(Engine)


    我想创建一个 car 的时候同时创建一个 engine,这样的 ModelSerializer 该怎样写?
    3 条回复    2018-11-30 08:53:18 +08:00
    mayorbryant
        1
    mayorbryant  
       2018-11-28 18:17:20 +08:00
    重写一个 Car 的 save 就行了啊
    Outliver0
        2
    Outliver0  
       2018-11-29 08:26:42 +08:00
    新建模型类重写 ModelSerializer 中的 create 方法,更新模型类重写 update 方法
    wxiao333
        3
    wxiao333  
    OP
       2018-11-30 08:53:18 +08:00
    @Outliver0 采用了这个方法,谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3075 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:32 · PVG 22:32 · LAX 07:32 · JFK 10:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.