解決済み: 日付の django 配列

日付の配列の主な問題は、操作が難しいことです。 日付の配列は単に日付のコレクションであるため、操作が難しい場合があります。

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=(日​​付,))

配列の操作

Django では、配列は単一の変数に複数の値を格納する便利な方法です。 配列は、通常の Python リストと同じ方法でインデックスを作成してアクセスできるため、小規模から中規模のアプリケーションにとって理想的なデータ構造になります。

Django で配列を作成するには、array() 関数を使用します。

my_array = 配列(1, 2, 3)

配列の個々の要素にアクセスするには、index() 関数を使用します。

print(my_array[0])
1を印刷します。 配列の要素を反復処理するには、for ループを使用します。

my_array の i の場合: print(i)

ワークウィットデーツ

Django では、work_with() 関数を使用して日付を管理できます。 この関数は、日付オブジェクトと作業オブジェクトの XNUMX つの引数を取ります。 日付オブジェクトは作業を開始する日付を表し、作業オブジェクトはその日に実行するタスクを表します。

たとえば、work_with() を使用して明日のタスクを作成できます。

work_with(date('tomorrow'), task('新しいブログ投稿を作成') )

Django の配列

Django の配列は、オブジェクトのコレクションです。 配列は 0 から始まるインデックスが付けられるため、0 つの要素を持つ配列は 1、2、3、4、XNUMX としてインデックス付けされます。

配列の最初の要素にアクセスするには、インデックス 0 を使用します。配列の最後の要素にアクセスするには、インデックス (n-1) を使用します。 配列内の他の要素にアクセスするには、インデックス n を使用します。

関連記事:

コメント