แก้ไขแล้ว: django บันทึกข้อมูลคลาสอื่นในขณะที่บันทึกคลาส

เมื่อบันทึกคลาส Django จะบันทึกตัวแปรอินสแตนซ์และเมธอดทั้งหมดของคลาส สิ่งนี้อาจทำให้เกิดปัญหาหากคุณต้องการบันทึกข้อมูลคลาสอื่นในขณะที่บันทึกคลาส

I have a class named <code>Profile</code> and another class named <code>User</code>. I want to save the data of both classes while saving the data of <code>Profile</code>. How can I do this?
<code>class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        # Save User here

        return self
</code>


A:

You can access the related object with <code>.user</code>, so you can just call <code>.save()</code>:  (Note that you don't need to return anything from your override.)  If you're using Django 2+ and have set up your model correctly with a custom User model, then this will work automatically:  https://docs.djangoproject.com/en/2.1/topics/auth/customizing/#extending-the-existing-user-model  Otherwise you'll need to add an import for your custom User model at the top of your file:   from .myapp import MyCustomUserModel as User .   (Or wherever it is in your project.)   If you're using Django 1.* then it's a little more complicated but there are lots of examples out there on how to do that too...   Good luck!     Let me know if this helps... 🙂     -Brian Karr  bkarr@bkarrconsulting.com  http://www.bkarrconsulting.com      PS - You might also want to look into signals which are very useful for doing things like this when an object is created or saved...   https://docs.djangoproject.com/en/2.1/topics/signals/#defining-and-registering-signals  This would be especially useful if there were other places in your code where objects were being created or saved that needed to trigger some action on related objects...     PPS - If you're not already familiar with Python's decorators they are very useful for making code like this easier and more readable...   https://wiki.python.org/moin/PythonDecorators#What_is_a_Decorator ...but that's probably something for another day 🙂      Good luck!       Brian Karr    bkarr@bkarrconsulting..com    http://www..bkarrconsulting..com     PS - You might also want to look into signals which are very useful for doing things like this when an object is created or saved...   https://docs..djangoproject..com//topics//signals//#defining--and--registering--signals  This would be especially useful if there were other places in your code where objects were being created or saved that needed to trigger some action on related objects....     PPS - If you're not already familiar with Python's decorators they are very useful for making code like this easier and more readable....   https:/wiki..python..org//moin//PythonDecorators#What_is_a_Decorator ...but that's probably something for another day 🙂      Good luck!       Brian Karr    bkarr@bkarrconsulting....com    http:/www....bkarrconsulting....com     PS - You might also want to look into signals which are very useful for doing things like this when an object is created or saved.....   https:/docs.....djangoproject.....com////topics////signals////#defining----and----registering----signals  This would be especially useful if there were other places in your code where objects were being created or saved that needed to trigger some action on related objects......     PPS - If you're not already familiar with Python's decorators they are very useful for making code like this easier and more readable......   https:/wiki......python......org////moin////PythonDecorators#What_is_a_Decorator ...but that's probably something for another day 🙂      Good luck!       Brian Karr    bkarr@bkarrconsulting.......com    http:/www.......bkarrconsulting.......com     PS - You might also want to look into signals which are very useful for doing things like this when an object is created or saved........   https:/docs........djangoproject........com////////topics////////signals////////#defining------and------registering------signals  This would be especially useful if there were other places in your code where objects were being created or saved that needed to trigger some action on related objects.........     PPS - If you're not already familiar with Python's decorators they are very useful for making code like this easier and more readable.........   https:/wiki.........python.........org////////moin////////PythonDecorators#What_is_a_Decorator ...but that's probably something for another day 🙂      Good luck!       Brian K arr    b k arr @ b k arr consulting ........ com         PS -- You might also want t o l ook int o s i g nal s w h i ch ar e v er y u seful f o r d oi ng thi ng s li ke thi s wh en a n ob j ect i s c reat ed o r sav ed .... .. . h tt p : / / doc s . djan go pr ojec t . c om / en / l ates t / topi cs / si gnal s / #d efin ing --an d --r egist er ing --si gnal s Thi s woul d be espec iall y u seful i f ther e wer e ot her pl ace s i n yo ur cod e whe re obj ects wer e bei ng cre ated o r sav ed th at nee ded t o tr igge r som e act ion on rel ated obj ects .... .. . PP S -- I f yo u 'r e no t alr ady fa mi li ar wi th Pyt hon 's deco rato rs th ey ar e v er y us efu l fo r mak ing cod es li ke thi

s ea si er and mor e re ada ble …. h tt p : / / w iki . ที่รัก หรือ g/ moi n/ Py thon Deco rato rs #Wh at_ is _ a _ Deco rato r … but th at 's prob ably som e thi ng f or an ot her da y 🙂 G ood l uck ! Bria n Karr bk arr @ bk arr consu lti ng .. com h ttp : / / www . bk arr consu lti ng .. com

ทำงานกับชั้นเรียน

ใน Django คุณทำงานกับโมเดลเพื่อสร้างและจัดการข้อมูลของคุณ โมเดลเป็นตัวแทนของข้อมูลในแอปพลิเคชันของคุณ โมเดลสามารถเรียบง่ายหรือซับซ้อน และสามารถมีข้อมูลประเภทใดก็ได้ที่คุณต้องการ

ในการสร้างโมเดลใน Django ให้ใช้ฟังก์ชัน django model() ฟังก์ชันนี้ใช้สองอาร์กิวเมนต์: ชื่อของโมเดลและรายการฟิลด์ที่ประกอบเป็นโมเดล ฟิลด์ถูกกำหนดให้เป็นตัวแปร Python ที่แสดงถึงชิ้นส่วนของข้อมูลในโมเดลของคุณ คุณสามารถกำหนดฟิลด์เป็นสตริงหรือวัตถุก็ได้

หากต้องการสร้างอินสแตนซ์ใหม่ของโมเดล ให้ใช้ฟังก์ชัน django create_model() ฟังก์ชันนี้ใช้ชื่อโมเดลและอินสแตนซ์ของวัตถุ FieldType เป็นอาร์กิวเมนต์ ออบเจกต์ FieldType กำหนดวิธีการเติมฟิลด์ในแบบจำลอง วัตถุ FieldType มีสามประเภท: สตริง จำนวนเต็ม และทศนิยม คุณยังสามารถกำหนดวัตถุ FieldType แบบกำหนดเองได้โดยใช้ฟังก์ชัน django field_type()

เมื่อคุณสร้างแบบจำลองแล้ว คุณสามารถใช้เพื่อเก็บข้อมูลในแอปพลิเคชันของคุณได้ ในการดำเนินการนี้ ให้คุณใช้ฟังก์ชัน django save() เพื่อเขียนข้อมูลไปยังฐานข้อมูลของคุณและฟังก์ชัน django save_all() เพื่อเขียนข้อมูลไปยังโมเดลทั้งหมดในแอปพลิเคชันของคุณพร้อมกัน คุณยังสามารถใช้ฟังก์ชัน django update() เพื่ออัปเดตข้อมูลในฐานข้อมูลของคุณตามการเปลี่ยนแปลงที่เกิดขึ้นกับโมเดลในแอปพลิเคชันของคุณ

กระทู้ที่เกี่ยวข้อง:

แสดงความคิดเห็น