V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Nov 28, 2018 · 2131 views
    This topic created in 2713 days ago, the information mentioned may be changed or developed.
    比如:
    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 replies    2018-11-30 08:53:18 +08:00
    mayorbryant
        1
    mayorbryant  
       Nov 28, 2018
    重写一个 Car 的 save 就行了啊
    Outliver0
        2
    Outliver0  
       Nov 29, 2018
    新建模型类重写 ModelSerializer 中的 create 方法,更新模型类重写 update 方法
    wxiao333
        3
    wxiao333  
    OP
       Nov 30, 2018
    @Outliver0 采用了这个方法,谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2372 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 10:31 · PVG 18:31 · LAX 03:31 · JFK 06:31
    ♥ Do have faith in what you're doing.