সমাধান করা হয়েছে: তারিখের জ্যাঙ্গো অ্যারে

তারিখের অ্যারেগুলির সাথে প্রধান সমস্যা হল যে তাদের সাথে কাজ করা কঠিন হতে পারে। তারিখের বিন্যাসগুলি কেবল তারিখের সংগ্রহ, এবং যেমন ম্যানিপুলেট করা কঠিন হতে পারে।

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 &lt;b&gt;&lt;i&gt;"my_dates"&lt;/i&gt;&lt;/b&gt;, 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 &lt;b&gt;&lt;i&gt;"my_dates"&lt;/i&gt;&lt;/b&gt; field is called &lt;"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(select={'my_dates': “SELECT * FROM my_dates WHERE my_dates = %s”}, select_params=(তারিখ,))

Arrays সঙ্গে কাজ

জ্যাঙ্গোতে, অ্যারেগুলি একটি একক ভেরিয়েবলে একাধিক মান সংরক্ষণ করার একটি সুবিধাজনক উপায়। অ্যারেগুলিকে নিয়মিত পাইথন তালিকার মতো একইভাবে সূচীকরণ এবং অ্যাক্সেস করা যেতে পারে, যা এগুলিকে ছোট থেকে মাঝারি আকারের অ্যাপ্লিকেশনগুলির জন্য একটি আদর্শ ডেটা কাঠামো তৈরি করে।

জ্যাঙ্গোতে একটি অ্যারে তৈরি করতে, অ্যারে() ফাংশন ব্যবহার করুন:

my_array = অ্যারে(1, 2, 3)

একটি অ্যারের পৃথক উপাদান অ্যাক্সেস করতে, index() ফাংশন ব্যবহার করুন:

মুদ্রণ(আমার_অ্যারে[0])
প্রিন্ট 1। একটি অ্যারের উপাদানগুলির উপর পুনরাবৃত্তি করতে, লুপ ব্যবহার করুন:

আমার_অ্যারেতে আমি জন্য: প্রিন্ট(i)

তারিখ বুঝে কাজ

জ্যাঙ্গোতে, আপনি তারিখগুলি পরিচালনা করতে work_with() ফাংশন ব্যবহার করতে পারেন। এই ফাংশনটি দুটি আর্গুমেন্ট নেয়: একটি তারিখ অবজেক্ট এবং একটি কাজের অবজেক্ট। তারিখ অবজেক্টটি সেই তারিখের প্রতিনিধিত্ব করে যেটি কাজটি শুরু করা উচিত এবং কাজের অবজেক্টটি সেই কাজটিকে প্রতিনিধিত্ব করে যা আপনি সেই তারিখে করতে চান।

উদাহরণস্বরূপ, আপনি আগামীকালের জন্য একটি টাস্ক তৈরি করতে work_with() ব্যবহার করতে পারেন:

work_with(তারিখ('আগামীকাল'), টাস্ক('নতুন ব্লগ পোস্ট তৈরি করুন'))

জ্যাঙ্গোতে অ্যারে

জ্যাঙ্গোতে একটি অ্যারে বস্তুর একটি সংগ্রহ। অ্যারেগুলি 0 থেকে শুরু করে ইন্ডেক্স করা হয়, তাই পাঁচটি উপাদান সহ একটি অ্যারে 0, 1, 2, 3, 4 হিসাবে সূচিত হবে।

একটি অ্যারের প্রথম উপাদান অ্যাক্সেস করতে, সূচক 0 ব্যবহার করুন। একটি অ্যারের মধ্যে শেষ উপাদান অ্যাক্সেস করতে, সূচক (n-1) ব্যবহার করুন। একটি অ্যারের অন্য কোনো উপাদান অ্যাক্সেস করতে, সূচক n ব্যবহার করুন.

সম্পর্কিত পোস্ট:

মতামত দিন