Using Django ArrayField to Store Dates and Value - django

I'm currently having a hard time wrapping my head around Django's Array Field. What i'm hoping to do is have an array that looks something like this:
Price(close=[
[1/1/2018, 3.00],
[1/2/2018, 1.00],
])
It's basically an array that stores a date followed by a corresponding value tied to that date. However, thus far my model looks like this:
class Price(models.Model):
close = ArrayField(
models.DecimalField(max_digits=10, decimal_places=4),
size=365,
)
I am not certain how to create an array with two different types of fields, one DateTime, the other decimal. Any help would be much appreciated.

You can't mix types stored in the ArrayField. [1] I recommend you to change model schema (aka Database normalization [2]).
This is my suggestion:
from django.db import models
class Price(models.Model):
pass
class PriceItem(models.Model):
datetime = models.DateTimeField()
ammount = models.DecimalField(max_digits=10, decimal_places=4)
price = models.ForeignKey(Price, on_delete=models.CASCADE)
[1] https://stackoverflow.com/a/8168017/752142
[2] https://en.wikipedia.org/wiki/Database_normalization

It depends on how important it is to the model.
postgresql provides composite types
The generous contributor of psycopg2 (django's posgresql driver) is supporting it,
define this type in postgresql:
CREATE TYPE date_price AS (
start date,
float8 price
);
and using the methods described here to implement CompositeField
from django.db import models
from django.contrib.postgres.fields import ArrayField
from django.db import connection
from psycopg2.extras import register_composite
# register the composite
register_composite('date_price', connection.cursor().cursor)
# CompositeField implementation here . . . . . .
class DatePriceField(CompositeField):
'''
DatePriceField specifics
'''
pass
class Price(models.Model):
close = ArrayField(base_field=DatePriceField(), size=365,)
I am going to follow this route and update soon.

Related

Django DateTimeRangeField: default=[timezone.now()]-[timezone.now()]+[10YEARS]

I want an "active_in" attribute as a timeframe. I assume that the DBMS is optimized for the postgresql tsrange field, and as such it is preferable to utilize the DateTimeRangeField rather than 2 separate fields for start_date and end_date.
Doing this I desire a default value for the field.
active_in = models.DateTimeRangeField(default=timezone.now+'-'+timezone.now+10YEARS)
Is my assumption about the DateTimeRangeField performance true?
Is there a smart solution be it creating a new; function,class or
simply manipulating the 2nd last digit?
My possible solutions:
Code using string manipulation:
active_in = models.DateTimeRangeField(default=timezone.now+'-'+timezone.now[:-2]+'30')
Code using custom function object: (adjusted from here: https://stackoverflow.com/a/27491426/7458018)
def today_years_ahead():
return timezone.now + '-' timezone.now() + timezone.timedelta(years=10)
class MyModel(models.Model):
...
active_in = models.DateTimeRangeField(default=today_years_ahead)
There's no need for string manipulation, as the documented Python type for this field is DateTimeTZRange.
I can't say I've ever used this field before, but something like this should work:
from psycopg2.extras import DateTimeTZRange
from django.utils import timezone
from datetime import timedelta
def next_ten_years():
now = timezone.now()
# use a more accurate version of "10 years" if you need it
return DateTimeTZRange(now, now + timedelta(days=3652))
class MyModel(models.Model):
...
active_in = models.DateTimeRangeField(default=next_ten_years)

Order Objects using an associated model and timedelta

I have a puzzle on my hands. As an exercise, I am trying to write a queryset that helps me visualize which of my professional contacts I should prioritize corresponding with.
To this end I have a couple of models:
class Person(models.Model):
name = models.CharField(max_length=256)
email = models.EmailField(blank=True, null=True)
target_contact_interval = models.IntegerField(default=45)
class ContactInstance(models.Model):
person = models.ForeignKey(Person, on_delete=models.CASCADE, related_name='contacts')
date = models.DateField()
notes = models.TextField(blank=True, null=True)
The column target_contact_interval on the Person model generally specifies the maximum amount of days that should pass before I reach out to this person again.
A ContactInstance reflects a single point of contact with a Person. A Person can have a reverse relationship with many ContactInstance objects.
So, the first Person in the queryset should own the greatest difference between the date of the most recent ContactInstance related to it and its own target_contact_interval
So my dream function would look something like:
Person.objects.order_by(contact__latest__date__day - timedelta(days=F(target_contact_interval))
but of course that won't work for a variety of reasons.
I'm sure someone could write up some raw PostgreSQL for this, but I am really curious to know if there is a way to accomplish it using only the Django ORM.
Here are the pieces I've found so far, but I'm having trouble putting them together.
I might be able to use a Subquery to annotate the date of the most recent datapoint:
from django.db.models import OuterRef, Subquery
latest = ContactInstance.objects.filter(person=OuterRef('pk')).order_by('-date')
Person.objects.annotate(latest_contact_date=Subquery(latest.values('date')[:1]))
And I like the idea of sorting the null values at the end:
from django.db.models import F
Person.objects.order_by(F('last_contacted').desc(nulls_last=True))
But I don't know where to go from here. I've been trying to put everything into order_by(), but I can't discern if it is possible to use F() with annotated values or with timedelta in my case.
UPDATE:
I have changed the target_contact_interval model to a DurationField as suggested. Here is the query I am attempting to use:
ci = ContactInstance.objects.filter(
person=OuterRef('pk')
).order_by('-date')
Person.objects.annotate(
latest_contact_date=Subquery(ci.values('date'[:1])
).order_by((
(datetime.today().date() - F('latest_contact_date')) -
F('target_contact_interval')
).desc(nulls_last=True))
It seems to me that this should work, however, the queryset is still not ordering correctly.

django query model A and exclude some items from related model B

I'm new to Django and I'm facing a question to which I didn't an answer to on Stackoverflow.
Basically, I have 2 models, Client and Order defined as below:
class Client(models.Model):
name = models.CharField(max_length=200)
registration_date = models.DateTimeField(default=timezone.now)
# ..
class Order(models.Model):
Client = models.ForeignKey(ModelA, on_delete=models.CASCADE, related_name='orders')
is_delivered = models.BooleanField(default=False)
order_date = models.DateTimeField(default=timezone.now)
# ..
I would like my QuerySet clients_results to fulfill the 2 following conditions:
Client objects fill some conditions (for example, their name start with "d" and they registered in 2019, but it could be more complex)
Order objects I can access by using the orders relationship defined in 'related_name' are only the ones that fills other conditions; for example, order is not delivered and was done in the last 6 weeks.
I could do this directly in the template but I feel this is not the correct way to do it.
Additionally, I read in the doc that Base Manager from Order shouldn't be used for this purpose.
Finally, I found a question relatively close to mine using Q and F, but in the end, I would get the order_id while, ideally, I would like to have the whole object.
Could you please advise me on the best way to address this need?
Thanks a lot for your help!
You probably should use a Prefetch(..) object [Django-doc] here to fetch the related non-delivered Orders for each Client, and stores these in the Clients, but then in a different attribute, since otherwise this can generate confusion.
You thus can create a queryset like:
from django.db.models import Prefetch
from django.utils.timezone import now
from datetime import timedelta
last_six_weeks = now() - timedelta(days=42)
clients_results = Client.objects.filter(
name__startswith='d'
).prefetch_related(
Prefetch(
'orders',
Order.objects.filter(is_delivered=False, order_date__gte=last_six_weeks),
to_attr='nondelivered_orders'
)
)
This will contain all Clients where the name starts with 'd', and each Client object that arises from this queryset will have an attribute nondelivered_orders that contains a list of Orders that are not delivered, and ordered in the last 42 days.

Query new Django/Postgres DateRangeField

Suppose I want to have a model like this:
from django.db import models
from django.contrib.postgres.fields import DateRangeField
class QueryRange(models.Model):
name = models.CharField('Name', max_length=50)
rsv = DateRangeField()
And in shell I create some objects like this:
obj1 = QueryRange.objects.create(name='John', rsv=('2016-06-01', '2016-06-10'))
obj2 = QueryRange.objects.create(name='Peter', rsv=('2016-07-05', '2016-07-10'))
obj3 = QueryRange.objects.create(name='Chris', rsv=('2016-07-12', '2016-07-15'))
How can I query the db by asking this question: Please check and see if the date i.e '2016-07-08' is occupied or not.
Something like this won't work:
from psycopg2.extras import DateRange
rng = DateRange('2016-07-08')
QueryRange.objects.filter(rsv__contains=rng)
I have implement the same scenario with two separate date fields (from_date and until_date) and works great (of course). I am just curious how can I benefit myself with the DateRangefield.
Cheers to all Djangonauts out there!
You're very close with your answer. When you're looking for the presence of a single date, use a date object directly:
from datetime import date
QueryRange.objects.filter(rsv__contains=date.today())
If you're using a date range to query, you're probably looking to see if there's any overlap:
rng = DateRange('2016-07-08', '2016-07-20')
QueryRange.objects.filter(rsv__overlap=rng)
Tested both examples to make sure they work.

Correct way to store multi-select results in Django model

all:
What is the correct Model fieldtype to use with a CheckboxSelectMultiple widget for static data? I am receiving validation errors and feel I'm missing something simple.
The app is a simple Django 1.6 app in which a Campground object can have multiple eligible_days (e.g. Site #123 may be available on Monday & Tuesday, while Site #456 is available on Weds-Friday).
Because it's static data and I've ready that a ManyToManyField has unnecessary DB overhead, I'm trying to do this with choices defined inside the model, but when I try to save I get the validation error Select a valid choice. [u'5', u'6'] is not one of the available choices. every time.
Q1: Do I have to override/subclass a field to support this?
Q2: Do I need a custom validation method to support this?
Q3: Am I making things unnecessarily hard on myself by avoiding ManyToManyField?
Thank you for your help!
/m
models.py
class CampgroundQuery(models.Model):
SUN = 0
MON = 1
TUE = 2
WED = 3
THU = 4
FRI = 5
SAT = 6
DAYS_OF_WEEK_CHOICES = (
(SUN, 'Sunday'),
(MON, 'Monday'),
(TUE, 'Tuesday'),
(WED, 'Wednesday'),
(THU, 'Thursday'),
(FRI, 'Friday'),
(SAT, 'Saturday'),
)
# loads choices from defined list
eligible_days = models.CharField(max_length=14,choices=DAYS_OF_WEEK_CHOICES,
blank=False, default='Saturday')
campground_id = models.SmallIntegerField()
stay_length = models.SmallIntegerField()
start_date = models.DateField()
end_date = models.DateField()
admin.py
from django.contrib import admin
from searcher.models import CampgroundQuery
from forms import CampgroundQueryAdminForm
class CampgroundQueryAdmin(admin.ModelAdmin):
form = CampgroundQueryAdminForm
admin.site.register(CampgroundQuery, CampgroundQueryAdmin)
forms.py
from django import forms
from django.contrib import admin
from searcher.models import CampgroundQuery
class CampgroundQueryAdminForm(forms.ModelForm):
class Meta:
model = CampgroundQuery
widgets = {
'eligible_days': forms.widgets.CheckboxSelectMultiple
}
I know this is an old question, but for those who wish to avoid using a ManyToManyField, there is a package to do this, django-multiselectfield, which is quick and easy to implement.
forms.py
from multiselectfield import MultiSelectFormField
class MyForm(forms.ModelForm):
my_field = MultiSelectFormField(choices=MyModel.MY_CHOICES)
models.py
from multiselectfield import MultiSelectField
class MyModel(models.Model):
MY_CHOICES = (
('a', "A good choice"),
...
('f', "A bad choice"),
)
my_field = MultiSelectField(choices=MY_CHOICES, max_length=11)
And that's it! It stores the keys of MY_CHOICES in a comma-separated string. Easy!
A ManyToManyField is the correct choice.
In theory you could create a compact representation, like a string field containing representations like "M,W,Th" or an integer that you'll set and interpret as seven binary bits, but that's all an immense amount of trouble to work with. ManyToManyFields are fine.