खोज…


वाक्य - विन्यास

  • django.contrib.postgres.fields से आयात * RangeField
  • IntegerRangeField (** विकल्प)
  • BigIntegerRangeField (** विकल्प)
  • FloatRangeField (** विकल्प)
  • DateTimeRangeField (** विकल्प)
  • DateRangeField (** विकल्प)

अपने मॉडल में संख्यात्मक रेंज फ़ील्ड शामिल हैं

पायथन में तीन प्रकार के संख्यात्मक RangeFieldIntegerField , BigIntegerField और FloatField । वे करने के लिए कनवर्ट psycopg2 NumericRange है, लेकिन देशी अजगर tuples के रूप में इनपुट स्वीकार करते हैं। निचली बाउंड को शामिल किया गया है और ऊपरी बाउंड को बाहर रखा गया है।

class Book(models.Model):
    name = CharField(max_length=200)
    ratings_range = IntegerRange()

रेंजफिल्ड की स्थापना

  1. अपने INSTALLED_APPS 'django.contrib.postgres' जोड़ें
  2. psycopg2 स्थापित करें

संख्यात्मक रेंज फ़ील्ड वाले मॉडल बनाना

यह एक NumericRange बजाय पायथन टपल के रूप में इनपुट मूल्यों के लिए सरल और आसान है।

Book.objects.create(name='Pro Git', ratings_range=(5, 5))

NumericRange साथ वैकल्पिक विधि:

Book.objects.create(name='Pro Git', ratings_range=NumericRange(5, 5))

का उपयोग कर शामिल हैं

यह क्वेरी तीन से कम रेटिंग वाली सभी पुस्तकों का चयन करती है।

bad_books = Books.objects.filter(ratings_range__contains=(1, 3))

उपयोग कर रहा है

इस क्वेरी को सभी किताबें शून्य से अधिक या छह से कम रेटिंग के साथ मिलती हैं।

all_books = Book.objects.filter(ratings_range_contained_by=(0, 6))

ओवरलैप का उपयोग करना

इस क्वेरी को छह से दस तक सभी ओवरलैपिंग अपॉइंटमेंट मिलते हैं।

Appointment.objects.filter(time_span__overlap=(6, 10))

कोई नहीं का उपयोग करने के लिए कोई ऊपरी बाध्य नहीं

यह क्वेरी सभी पुस्तकों को चार से अधिक या उसके बराबर किसी भी रेटिंग के साथ चुनती है।

maybe_good_books = Books.objects.filter(ratings_range__contains=(4, None))

रंग संचालन

from datetime import timedelta

from django.utils import timezone
from psycopg2.extras import DateTimeTZRange

# To create a "period" object we will use psycopg2's DateTimeTZRange
# which takes the two datetime bounds as arguments
period_start = timezone.now()
period_end = period_start + timedelta(days=1, hours=3)
period = DateTimeTZRange(start, end)

# Say Event.timeslot is a DateTimeRangeField

# Events which cover at least the whole selected period,
Event.objects.filter(timeslot__contains=period)

# Events which start and end within selected period,
Event.objects.filter(timeslot__contained_by=period)

# Events which, at least partially, take place during the selected period.
Event.objects.filter(timeslot__overlap=period)


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow