हल किया गया: 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 d mor e re ada ble… . एच टीटी पी : // डब्ल्यू iki . माननीय। or g/ moi n/ Py thon Deco rato rs #Wh at_ is_ a_ Deco rato r ... but th at's probly som e thi ng f or an ot her day 🙂 Good l uck! Bria n Karr bk arr @ bk arr consu lti ng..com h ttp: // www। bk arr उपभोक्ता lti एनजी .. com

वर्ग के साथ काम करें

Django में, आप अपना डेटा बनाने और प्रबंधित करने के लिए मॉडल के साथ काम करते हैं। एक मॉडल आपके आवेदन में डेटा का प्रतिनिधित्व है। मॉडल सरल या जटिल हो सकते हैं, और इसमें आपके लिए आवश्यक किसी भी प्रकार का डेटा हो सकता है।

Django में एक मॉडल बनाने के लिए, आप django मॉडल () फ़ंक्शन का उपयोग करते हैं। यह फ़ंक्शन दो तर्क लेता है: मॉडल का नाम और फ़ील्ड की एक सूची जो मॉडल बनाती है। फ़ील्ड्स को Python वेरिएबल्स के रूप में परिभाषित किया गया है जो आपके मॉडल में डेटा के टुकड़ों का प्रतिनिधित्व करते हैं। आप फ़ील्ड्स को या तो स्ट्रिंग्स या ऑब्जेक्ट के रूप में परिभाषित कर सकते हैं।

मॉडल का एक नया उदाहरण बनाने के लिए, आप django create_model() फ़ंक्शन का उपयोग करते हैं। यह फ़ंक्शन अपने तर्कों के रूप में मॉडल का नाम और फ़ील्डटाइप ऑब्जेक्ट का एक उदाहरण लेता है। फील्डटाइप ऑब्जेक्ट परिभाषित करता है कि मॉडल में फ़ील्ड कैसे भरे जाने चाहिए। तीन प्रकार के फील्डटाइप ऑब्जेक्ट हैं: स्ट्रिंग, पूर्णांक और फ्लोट। आप django field_type() फ़ंक्शन का उपयोग करके कस्टम फ़ील्डटाइप ऑब्जेक्ट्स को भी परिभाषित कर सकते हैं।

एक बार जब आप एक मॉडल बना लेते हैं, तो आप इसका उपयोग अपने एप्लिकेशन में डेटा स्टोर करने के लिए कर सकते हैं। ऐसा करने के लिए, आप अपने डेटाबेस में डेटा लिखने के लिए django सेव () फ़ंक्शन का उपयोग करते हैं और django save_all () फ़ंक्शन का उपयोग आपके एप्लिकेशन में सभी मॉडलों को एक बार में डेटा लिखने के लिए करते हैं। आप अपने एप्लिकेशन में मॉडल में किए गए परिवर्तनों के आधार पर अपने डेटाबेस में डेटा को अपडेट करने के लिए django अपडेट () फ़ंक्शन का भी उपयोग कर सकते हैं।

संबंधित पोस्ट:

एक टिप्पणी छोड़ दो