RE: Run model check with Django 1.11 as deployment check only?

2017-04-10 Thread Matthew Pava
Hi Carsten,
I wish I could provide more help.  I had no idea there was a check method or a 
system check framework until your message.
https://docs.djangoproject.com/en/1.11/topics/checks/

I have learned something new.

One thing mentioned in the topic guide above is this:
If you need to run system checks on your deployment server, trigger them 
explicitly using check.
The check command documentation is here:
https://docs.djangoproject.com/en/1.11/ref/django-admin/#django-admin-check

Hoping that helps,
Matthew



-Original Message-
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Carsten Fuchs
Sent: Monday, April 10, 2017 11:08 AM
To: django-users@googlegroups.com
Subject: Re: Run model check with Django 1.11 as deployment check only?

Hi Matthew,

Am 10.04.2017 um 15:51 schrieb Matthew Pava:
> I would argue that your code should be able to run successfully with an empty 
> database.
> I would change your assignment of maxID to this:
> maxID = cls.objects.aggregate(Max('id'))['id__max'] or 0

Normally, I would happily agree. However, in this case, the Status objects are 
static and never supposed to change (without reloading the entire project).

Based on this assumption, all that I want is a constant `MAX_ID` that reflects 
the largest possible ID of the existing Status objects, so that frequent access 
to it does not each time inflict a database access.

Therefore, my older code just had a module global variable like this:

MAX_ID = Status.objects.aggregate(Max('id'))['id__max']

This is simple and "usually" works well, but note that it fails whenever an 
empty database is initialized, that is, whenever the very first migration is 
run (in which case the above global code is run even before the Status table 
exists), which in turn happens when the project is deployed to a new site or 
whenever the test database is populated.

In summary:

Code that is run at load time (such as the global `MAX_ID = ...` above) or 
close to load time (such as the Django system checks) cannot access the 
database if the database is only populated at a later time, as is the case with 
initial migrations for new deployments or tests databases.

What I would like to check, though, is the production or development databases; 
thus my question for marking the model check „for deployment only“.

Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/acc78088-9d44-97ee-915a-a9ad40974ff8%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/4ab43916b5fd421c877fe35ba7bc5ae9%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.


Re: Run model check with Django 1.11 as deployment check only?

2017-04-10 Thread Carsten Fuchs

Hi Matthew,

Am 10.04.2017 um 15:51 schrieb Matthew Pava:

I would argue that your code should be able to run successfully with an empty 
database.
I would change your assignment of maxID to this:
maxID = cls.objects.aggregate(Max('id'))['id__max'] or 0


Normally, I would happily agree. However, in this case, the Status objects are 
static and never supposed to change (without reloading the entire project).


Based on this assumption, all that I want is a constant `MAX_ID` that reflects 
the largest possible ID of the existing Status objects, so that frequent access 
to it does not each time inflict a database access.


Therefore, my older code just had a module global variable like this:

MAX_ID = Status.objects.aggregate(Max('id'))['id__max']

This is simple and "usually" works well, but note that it fails whenever an 
empty database is initialized, that is, whenever the very first migration is run 
(in which case the above global code is run even before the Status table 
exists), which in turn happens when the project is deployed to a new site or 
whenever the test database is populated.


In summary:

Code that is run at load time (such as the global `MAX_ID = ...` above) or close 
to load time (such as the Django system checks) cannot access the database if 
the database is only populated at a later time, as is the case with initial 
migrations for new deployments or tests databases.


What I would like to check, though, is the production or development databases; 
thus my question for marking the model check „for deployment only“.


Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/acc78088-9d44-97ee-915a-a9ad40974ff8%40cafu.de.
For more options, visit https://groups.google.com/d/optout.


RE: Run model check with Django 1.11 as deployment check only?

2017-04-10 Thread Matthew Pava
I would argue that your code should be able to run successfully with an empty 
database.
I would change your assignment of maxID to this:
maxID = cls.objects.aggregate(Max('id'))['id__max'] or 0

I often add the 'or 0' to my aggregates because I never really know if my 
aggregate will deal with an empty QuerySet.

-Original Message-
From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On 
Behalf Of Carsten Fuchs
Sent: Monday, April 10, 2017 8:38 AM
To: Django users
Subject: Run model check with Django 1.11 as deployment check only?

Dear Django group,

with Django 1.10, I used a model check like this:


class Status(models.Model):
 id = models.AutoField(primary_key=True)
 MAX_ID = 38
 text   = models.CharField(max_length=20, blank=True)

 @classmethod
 def check(cls, **kwargs):
 errors = super(Status, cls).check(**kwargs)
 maxID = cls.objects.aggregate(Max('id'))['id__max']

 # The app sometimes computes histograms,
 # so make sure that the Status.id values are as expected.
 if cls.MAX_ID != maxID:
 errors.append(checks.Warning(...))

 return errors


This used to work well, considering that up to Django 1.10, this and other 
checks weren't run along with `manage.py test`.
Now that Django 1.11 runs checks also with tests, the above check obviously 
fails, because at the time the checks are run, the test database is still 
empty, yielding `maxID = None`.

Declaring the above check as “at deployment only” seems to be a good 
resolution, but is it possible to do that with a model (base-class based) check?

Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/03fdc69d-0ac8-b266-5533-d00301f9fdce%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5c5d952d3e7e4147963f196a64722d3b%40ISS1.ISS.LOCAL.
For more options, visit https://groups.google.com/d/optout.


Run model check with Django 1.11 as deployment check only?

2017-04-10 Thread Carsten Fuchs

Dear Django group,

with Django 1.10, I used a model check like this:


class Status(models.Model):
id = models.AutoField(primary_key=True)
MAX_ID = 38
text   = models.CharField(max_length=20, blank=True)

@classmethod
def check(cls, **kwargs):
errors = super(Status, cls).check(**kwargs)
maxID = cls.objects.aggregate(Max('id'))['id__max']

# The app sometimes computes histograms,
# so make sure that the Status.id values are as expected.
if cls.MAX_ID != maxID:
errors.append(checks.Warning(...))

return errors


This used to work well, considering that up to Django 1.10, this and other 
checks weren't run along with `manage.py test`.
Now that Django 1.11 runs checks also with tests, the above check obviously 
fails, because at the time the checks are run, the test database is still empty, 
yielding `maxID = None`.


Declaring the above check as “at deployment only” seems to be a good resolution, 
but is it possible to do that with a model (base-class based) check?


Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/03fdc69d-0ac8-b266-5533-d00301f9fdce%40cafu.de.
For more options, visit https://groups.google.com/d/optout.