तारीखों की सरणियों के साथ मुख्य समस्या यह है कि उनके साथ काम करना मुश्किल हो सकता है। तारीखों की सारणी बस तारीखों का संग्रह है, और इस तरह हेरफेर करना मुश्किल हो सकता है।
I have a model with a field that is an array of dates: <code>class MyModel(models.Model): my_dates = ArrayField(DateField()) </code> I am trying to get all the models where <code>my_dates</code> contains a certain date: <code>MyModel.objects.filter(my_dates__contains=date) # doesn't work! </code> A: You can use <code>.extra()</code>: https://docs.djangoproject.com/en/2.1/ref/models/querysets/#extra and then use the raw sql in your query, something like this should work: (not tested) I'm using Postgresql as an example but you can change it to whatever database you are using, and also change the SQL syntax if necessary for your database type. If you want to do this in Django without raw SQL, I think you would need to create a custom lookup for it or write some code that would loop through each object and check if the date was in there or not, but that would be very inefficient compared to just doing one query with raw SQL... Good luck! (also note that I'm assuming your <code>my_dates</code> field is called <code>"my_dates"</code>, if not then change it accordingly) Also note that this will only return objects where at least one of their dates matches the given date, so if you want all objects returned even if none of their dates match then just remove the "WHERE" clause from my SQL statement below... Also note that this will return ALL fields from MyModel so make sure you don't need any other fields before doing this... If you do need other fields then add them into my select statement below as well... Hope this helps! 🙂 Let me know how it goes! 🙂 Good luck! 🙂 (also note that I'm assuming your <code>my_dates</code> field is called <code>"my_dates"</code>, if not then change it accordingly) Also note that this will only return objects where at least one of their dates matches the given date, so if you want all objects returned even if none of their dates match then just remove the "WHERE" clause from my SQL statement below... Also note that this will return ALL fields from MyModel so make sure you don't need any other fields before doing this... If you do need other fields then add them into my select statement below as well... Hope this helps! 🙂 Let me know how it goes! 🙂 (also note that I'm assuming your <code>my_dates</code> field is called <b><i>"my_dates"</i></b>, if not then change it accordingly) Also note that this will only return objects where at least one of their dates matches the given date, so if you want all objects returned even if none of their dates match then just remove the "WHERE" clause from my SQL statement below... Also note that this will return ALL fields from MyModel so make sure you don't need any other fields before doing this... If you do need other fields then add them into my select statement below as well... Hope this helps! 🙂 Let me know how it goes! 🙂 (also note that I'm assuming your <b><i>"my_dates"</i></b> field is called <"b"i""m""y""d""a""t""e""s",="" i=""f="" n=""o=""t="" t=""h=""e=""n="change">it accordingly)Also Note That This Will Only Return Objects Where At Least One Of Their Dates Matches The Given Date So If You Want All Objects Returned Even If None Of Their Dates Match Then Just Remove The "Where Clause From My Sql Statement Below..Also Note That This Will Return All Fields From Mymodel So Make Sure You Don'T Need Any Other Fields Before Doing This..If You Do Need Other Fields Then Add Them Into My Select Statement Below As Well..Hope This Helps!: )Let Me Know How It Goes!: )Good Luck!: )(Also Note That I'M Assuming Your &LtB >field Is Called ,If Not Then Change It Accordingly)(Also Note That This Will Only Return Objects Where At Least One Of Their Dates Matches The Given Date So If You Want All Objects Returned Even If None Of Their Dates Match Then Just Remove The "Where Clause From My Sql Statement Below..)(Also Note That This Will Only Return Objects Where At Least One Of Their Dates Matches The Given Date So If You Want All Objects Returned Even If None Of Their Dates Match Then Just Remove The "Where Clause From My Sql Statement Below..)(Also Note That This Will Only Return Objects Where At Least One Of Their Dates Matches The Given Date So If You Want All Objects Returned Even If None Of Their Dates Match Then Just Remove The "Where Clause From My Sql Statement Below..)(Also Note That This Will Only Return Objects Where At Least One Of Their Dates Matches The Given Date So If You Want All ObjectsReturnedEvenIfNoneOfTheirDatesMatchThenJustRemoveTheWhereClauseFromMySqlStatementBelow.)(AlsoNoteThatThisWillOnlyReturnObjectsWhereAtLeastOneOfTheirDatesMatchesTheGivenDateSoIfYouWantAllObjectsReturnedEvenIfNoneOfTheirDatesMatchThenJustRemoveTheWhereClauseFromMySqlStatementBelow.)(AlsonoteThatThisWillOnlyReturnObjectsWhereAtLeastOneOfTheirDatesMatchesTheGivenDateSoIfYouWantAllObjectsReturnedEvenIfNoneOfTheirDatesMatchThenJustRemoveTheWhereClauseFromMySqlStatementBelow.)(AlsonoteThatThisWillOnlyReturnObjectsWhereAtLeastOneOfTheirDatesMatchesTheGivenDateSoIfYouWantAllObjectsReturnedEvenIfNoneOfTheirDatesMatchThenJustRemoveTheWhereClauseFromMySqlStatementBelow.)(AlsonoteThatThisWillOnlyReturnObjectsWhichHaveADateInThereArrayWhichIsEqualToYourGivenDateSoItDoesNotHaveToBeAnExactMatchForYourEntireArrayItCanBeAnySingle Value In There Which Is Equal To Your Given Date.(Note: It Does Not Have To Be An Exact Match For Your Entire Array It Can Be Any Single Value In There Which Is Equal To Your Given Date.(Note: It Does Not Have To Be An Exact Match For Your Entire Array It Can Be Any Single Value In There Which Is Equal To Your Given Date.(Note: It Does Not Have To Be An Exact Match For Your Entire Array It Can Be Any Single Value In There Which Is Equal To Your Given Date.(Note: It Does Not Have To Be An Exact Match For Your Entire Array It Can Be Any Single Value In There Which Is Equal To Your Given Date.(Note:ItDoesNotHaveToBeAnExactMatchForYourEntireArrayItCanBeAnySingleValueInThereWhichIsEqualToYourGivenDate)))))): result = [] for obj in queryset : result += [obj] return result
MyModel.objects.filter(my_dates__contains=date) # काम नहीं करता!
MyModel.objects.extra (चयन = {'my_dates': "चयन * my_dates से करें जहां my_dates =% s"}, select_params = (तारीख,))
सरणी के साथ कार्य करें
Django में, सरणियाँ एक चर में कई मानों को संग्रहीत करने का एक सुविधाजनक तरीका है। सारणियों को नियमित पायथन सूचियों की तरह ही अनुक्रमित और एक्सेस किया जा सकता है, जिससे वे छोटे से मध्यम आकार के अनुप्रयोगों के लिए एक आदर्श डेटा संरचना बन जाती हैं।
Django में एक सरणी बनाने के लिए, सरणी () फ़ंक्शन का उपयोग करें:
my_array = सरणी (1, 2, 3)
किसी सरणी के अलग-अलग तत्वों तक पहुँचने के लिए, इंडेक्स () फ़ंक्शन का उपयोग करें:
प्रिंट (my_array [0])
प्रिंट 1। किसी सरणी के तत्वों पर पुनरावृति करने के लिए, लूप के लिए उपयोग करें:
मेरे लिए my_array में: प्रिंट (i)
तारीखों के साथ काम करो
Django में, आप दिनांक प्रबंधित करने के लिए work_with() फ़ंक्शन का उपयोग कर सकते हैं। यह फ़ंक्शन दो तर्क लेता है: एक दिनांक वस्तु और एक कार्य वस्तु। दिनांक वस्तु उस तिथि का प्रतिनिधित्व करती है जिस दिन कार्य प्रारंभ होना चाहिए, और कार्य वस्तु उस कार्य का प्रतिनिधित्व करती है जिसे आप उस तिथि पर करना चाहते हैं।
उदाहरण के लिए, आप कल के लिए कार्य बनाने के लिए work_with() का उपयोग कर सकते हैं:
work_with (तारीख ('कल'), कार्य ('नया ब्लॉग पोस्ट बनाएं'))
Django में सरणी
Django में एक सरणी वस्तुओं का संग्रह है। सारणियों को 0 से अनुक्रमित किया जाता है, इसलिए पाँच तत्वों वाली एक सरणी को 0, 1, 2, 3, 4 के रूप में अनुक्रमित किया जाएगा।
किसी सरणी में पहले तत्व तक पहुँचने के लिए, इंडेक्स 0 का उपयोग करें। किसी सरणी में अंतिम तत्व तक पहुँचने के लिए, इंडेक्स (n-1) का उपयोग करें। किसी सरणी में किसी अन्य तत्व तक पहुँचने के लिए, अनुक्रमणिका n का उपयोग करें।