samedi 29 novembre 2014

Android - Framework Django reste : afficher directement sur la liste des résultats en GenericView - Stack Overflow


I'm using djangorestframework to manage a REST API that connects an Android mobile app to my Django web application. I have a list of objects that I need to retrieve from the web app through the REST API, so far a generic ListCreateAPIView works fine.


However, what the REST API returns isn't exactly a list/array per se, but a JSON object containing metadata and the actual results list. Here's an example of the said output:


{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"foo":"bar"
}
]
}

The problem is, my mobile app's REST client expects a JSON list/array, not the JSON object above. Is there a way to make my generic view remove the count, next, and previous metadata and just output the results list itself? I need an output like the following:


[
{"foo":"bar"},
{"foo":"something"},
{"foo":"another"}
]

Oh, and I'm not sure if this would be helpful, but I use Retrofit as a REST client for my Android app, which is supposed to connect to my web app's REST API.




This object that wraps the array is generated by the queryset paginator. If you disable pagination you will get the array. To disable pagination, set paginate_by to None:


class PaginatedListView(ListAPIView):
queryset = ExampleModel.objects.all()
serializer_class = ExampleModelSerializer
paginate_by = None


I'm using djangorestframework to manage a REST API that connects an Android mobile app to my Django web application. I have a list of objects that I need to retrieve from the web app through the REST API, so far a generic ListCreateAPIView works fine.


However, what the REST API returns isn't exactly a list/array per se, but a JSON object containing metadata and the actual results list. Here's an example of the said output:


{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"foo":"bar"
}
]
}

The problem is, my mobile app's REST client expects a JSON list/array, not the JSON object above. Is there a way to make my generic view remove the count, next, and previous metadata and just output the results list itself? I need an output like the following:


[
{"foo":"bar"},
{"foo":"something"},
{"foo":"another"}
]

Oh, and I'm not sure if this would be helpful, but I use Retrofit as a REST client for my Android app, which is supposed to connect to my web app's REST API.



This object that wraps the array is generated by the queryset paginator. If you disable pagination you will get the array. To disable pagination, set paginate_by to None:


class PaginatedListView(ListAPIView):
queryset = ExampleModel.objects.all()
serializer_class = ExampleModelSerializer
paginate_by = None

Python 2.7 - Django reproduisant un modèle objet causant problème - Stack Overflow


I have a 2 models with a foreign/Primary key to same model.


model Foo:
FK(Too, pk)
model Coo:
FK(Too, pk)
model Too:
blah = charfield()

In the views I am seeing some very strange behavior. I think I am doing something very wrong. I want to replicate a object of Too and then save it. For e.g.


too = Too.create(blah="Awesome")
too.save()
foo = Foo.create(too=too)
foo.save()
too.id = None #Copy the original
too.save()
coo = Coo.create(too=too)
coo.save()
print foo.too.id
print coo.too.id
#above 2 print statements give same id

When I check in the admin the both foo and coo have different too object saved. But while printing it is showing the same. Why is that happening. I think I am doing something fundamentally wrong.






Django looks at the primary key to determine uniqueness, so work with that directly:



too.pk = None
too.save()

Setting the primary key to None will cause Django to perform an INSERT, saving a new instance of the model, rather than an UPDATE to the existing instance.





Source:
http://stackoverflow.com/a/4736172/1533388


UPDATE: err, using pk and id are interchangeable in this case, so you'll get the same result. My first answer didn't address your question.


The discrepancy here is between what is occurring in python vs. what can be reconstituted from the database.


Your code causes Django to save two unique objects to the database, but you're only working with one python Too instance. When foo.save() occurs, the database entry for 'foo' is created with a reference to the DB entry for the first Too object. When coo.save() occurs, the database entry for 'coo' is created, pointing to the second, unique Too object that was stored via:


too.id = None #Copy the original 
too.save()

However, in python, both coo and foo refer to the same object, named 'too', via their respective '.too' attributes. In python, there is only one 'Too' instance. So when you update too.id, you're updating one object, referred to by both coo and foo.


Only when the models are reconstituted from the database (as the admin view does in order to display them) are unique instances created for each foreign key; this is why the admin view shows two unique saved instances.



I have a 2 models with a foreign/Primary key to same model.


model Foo:
FK(Too, pk)
model Coo:
FK(Too, pk)
model Too:
blah = charfield()

In the views I am seeing some very strange behavior. I think I am doing something very wrong. I want to replicate a object of Too and then save it. For e.g.


too = Too.create(blah="Awesome")
too.save()
foo = Foo.create(too=too)
foo.save()
too.id = None #Copy the original
too.save()
coo = Coo.create(too=too)
coo.save()
print foo.too.id
print coo.too.id
#above 2 print statements give same id

When I check in the admin the both foo and coo have different too object saved. But while printing it is showing the same. Why is that happening. I think I am doing something fundamentally wrong.





Django looks at the primary key to determine uniqueness, so work with that directly:



too.pk = None
too.save()

Setting the primary key to None will cause Django to perform an INSERT, saving a new instance of the model, rather than an UPDATE to the existing instance.





Source:
http://stackoverflow.com/a/4736172/1533388


UPDATE: err, using pk and id are interchangeable in this case, so you'll get the same result. My first answer didn't address your question.


The discrepancy here is between what is occurring in python vs. what can be reconstituted from the database.


Your code causes Django to save two unique objects to the database, but you're only working with one python Too instance. When foo.save() occurs, the database entry for 'foo' is created with a reference to the DB entry for the first Too object. When coo.save() occurs, the database entry for 'coo' is created, pointing to the second, unique Too object that was stored via:


too.id = None #Copy the original 
too.save()

However, in python, both coo and foo refer to the same object, named 'too', via their respective '.too' attributes. In python, there is only one 'Too' instance. So when you update too.id, you're updating one object, referred to by both coo and foo.


Only when the models are reconstituted from the database (as the admin view does in order to display them) are unique instances created for each foreign key; this is why the admin view shows two unique saved instances.


Django : Restitue un modèle en bloc sans charger la page entière - Stack Overflow


I have a short question and i am sure there is a simply answer. I have a template called base.html which looks simplified like this:


<html>
<head>
<title>{% block title %} Title {% endblock %}</title>
</head>
<body>
<div class="header">
{% block header %}My Header{% endblock %}
</div>

<div class="navigation">
{% block navigation %}My Navi{% endblock %}
</div>

<div class="content">
{% block content %}Content{% endblock %}
</div>
</body>
</html>

When i click on a link in the navi i want to render the new content in the content block without loading the header and die navigation again.


Thanks




You have a couple of choices: Ajax or iFrames


If you go the Ajax route, you can render a template server-side and return the HTML in the Ajax response:


import json
from django.http import HttpResponse
from django.template.loader import render_to_string

def render_template(request):
result = {'html': render_to_string('your-template.html', {})}
return HttpResponse(json.dumps(result, ensure_ascii=False),
content_type='application/json')

Then you can use jQuery or whatever you want to update the HTML in the main page wherever you need.


Alternatively, you can use JavaScript to set the src of an iFrame on click to render a view without updating the entire page. It just depends on what's best for you in terms of user experience, maintenance, etc.



I have a short question and i am sure there is a simply answer. I have a template called base.html which looks simplified like this:


<html>
<head>
<title>{% block title %} Title {% endblock %}</title>
</head>
<body>
<div class="header">
{% block header %}My Header{% endblock %}
</div>

<div class="navigation">
{% block navigation %}My Navi{% endblock %}
</div>

<div class="content">
{% block content %}Content{% endblock %}
</div>
</body>
</html>

When i click on a link in the navi i want to render the new content in the content block without loading the header and die navigation again.


Thanks



You have a couple of choices: Ajax or iFrames


If you go the Ajax route, you can render a template server-side and return the HTML in the Ajax response:


import json
from django.http import HttpResponse
from django.template.loader import render_to_string

def render_template(request):
result = {'html': render_to_string('your-template.html', {})}
return HttpResponse(json.dumps(result, ensure_ascii=False),
content_type='application/json')

Then you can use jQuery or whatever you want to update the HTML in the main page wherever you need.


Alternatively, you can use JavaScript to set the src of an iFrame on click to render a view without updating the entire page. It just depends on what's best for you in terms of user experience, maintenance, etc.


la base de données - Django, utilisateur en relation avec une autre table - Stack Overflow


So I got the tables you can see in the image below:


database.


What I would like to do is to create a relationship so that each user (of django auth_user) will be enrolled(or able to enrol) to exactly one "course" so that he will be able to see next events for his modules.


Do I have to create another table and place 2 foreign keys or this is a way to do it in 'php' and it's more simple with Django? I was suggested to create 'student' model inheriting from 'User' with extended behavior and one to many relationship on auth. I tried to do that but unfortunately had not results since I'm really new to Django & Python.




If every auth_user (or auth.User) will be or have the opportunity to be enrolled on a course I would create a 'user profile' model that has a 1-to-1 relationship with the django User model. You can store additional User data in this model, including what course they are enrolled on. See https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model for more details but here is an example:


class UserProfile(models.Model):
user = models.OneToOneField('auth.User')
course = models.ForeignKey('courseapp.Course', null=True)

You would probably need to create a signal that gets fired each time an auth.User object is saved, such that if it is the first time that User object has been saved, it automatically creates the UserProfile:


from django.contrib.auth.models import User
from django.db.models.signals import post_save
from yourusersapp.models import UserProfile

def create_user_profile(sender, instance, created, **kwargs):
# Automatically creates a UserProfile on User creation.
if created:
UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

When you query a User object, you can then reference the User object's profile like:


user_object.userprofile

You could then create a Course object and link the user_object indirectly via its UserProfile to that Course:


course = Course.objects.create(name='course_name', next_field='whatever')
user_profile = user_object.userprofile
userprofile.course = course
userprofile.save()

Now you have a user object with a UserProfile that is linked to only 1 course. Many users can be on the same course, but a user can only be on 1 course. You can also reference all users on a particular course like:


course = Course.objects.get(name='course_name')
course_users = course.userprofile_set.all()

HTH




I think that you can go about this one of two ways.



  1. Extend the User model. 'Student' would probably be a good name for your new model. It would have a OneToOne relationship with 'User', and a ForeignKey relationship with 'Course'. It can store any other information that is applicable to students only. Documentation for how to do that can be found here https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#extending-the-existing-user-model


  2. Create a custom User model that has a ForeignKey relationship with Course. This approach is a bit more complicated, but yields a slightly cleaner end result. Documentation for that is here. https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#substituting-a-custom-user-model



Sorry if it seems like I'm just sending you to the Django docs, but both of those sections are well written and should explain things pretty clearly. If you'd like to post another question with example code we can try and see why your original attempt at extending the User model didn't work. By the way, your "Student" model shouldn't have to inherit from the User model in order to extend it.



So I got the tables you can see in the image below:


database.


What I would like to do is to create a relationship so that each user (of django auth_user) will be enrolled(or able to enrol) to exactly one "course" so that he will be able to see next events for his modules.


Do I have to create another table and place 2 foreign keys or this is a way to do it in 'php' and it's more simple with Django? I was suggested to create 'student' model inheriting from 'User' with extended behavior and one to many relationship on auth. I tried to do that but unfortunately had not results since I'm really new to Django & Python.



If every auth_user (or auth.User) will be or have the opportunity to be enrolled on a course I would create a 'user profile' model that has a 1-to-1 relationship with the django User model. You can store additional User data in this model, including what course they are enrolled on. See https://docs.djangoproject.com/en/dev/topics/auth/customizing/#extending-the-existing-user-model for more details but here is an example:


class UserProfile(models.Model):
user = models.OneToOneField('auth.User')
course = models.ForeignKey('courseapp.Course', null=True)

You would probably need to create a signal that gets fired each time an auth.User object is saved, such that if it is the first time that User object has been saved, it automatically creates the UserProfile:


from django.contrib.auth.models import User
from django.db.models.signals import post_save
from yourusersapp.models import UserProfile

def create_user_profile(sender, instance, created, **kwargs):
# Automatically creates a UserProfile on User creation.
if created:
UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

When you query a User object, you can then reference the User object's profile like:


user_object.userprofile

You could then create a Course object and link the user_object indirectly via its UserProfile to that Course:


course = Course.objects.create(name='course_name', next_field='whatever')
user_profile = user_object.userprofile
userprofile.course = course
userprofile.save()

Now you have a user object with a UserProfile that is linked to only 1 course. Many users can be on the same course, but a user can only be on 1 course. You can also reference all users on a particular course like:


course = Course.objects.get(name='course_name')
course_users = course.userprofile_set.all()

HTH



I think that you can go about this one of two ways.



  1. Extend the User model. 'Student' would probably be a good name for your new model. It would have a OneToOne relationship with 'User', and a ForeignKey relationship with 'Course'. It can store any other information that is applicable to students only. Documentation for how to do that can be found here https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#extending-the-existing-user-model


  2. Create a custom User model that has a ForeignKey relationship with Course. This approach is a bit more complicated, but yields a slightly cleaner end result. Documentation for that is here. https://docs.djangoproject.com/en/1.6/topics/auth/customizing/#substituting-a-custom-user-model



Sorry if it seems like I'm just sending you to the Django docs, but both of those sections are well written and should explain things pretty clearly. If you'd like to post another question with example code we can try and see why your original attempt at extending the User model didn't work. By the way, your "Student" model shouldn't have to inherit from the User model in order to extend it.


django-registration 1.0 avec 1.6 Django et Python 3.3.4 - Stack Overflow


I'm trying to upgrade a webapp called courses from Django 1.4 to 1.6 with django-registration 1.0. None of the other django-registration threads seem to have anything relating to this problem.


I'm running unit-tests via python manage.py test courses which crashes with the following trace:


> /home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/transaction.py(449)commit_on_success()
-> warnings.warn("commit_on_success is deprecated in favor of atomic.",
(Pdb) c
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/test/runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/test/runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/test/runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/backends/creation.py", line 339, in create_test_db
load_initial_data=False)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 284, in execute
self.validate()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/models/loading.py", line 75, in _populate
self.load_app(app_name, True)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/models/loading.py", line 99, in load_app
models = import_module('%s.models' % app_name)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
File "<frozen importlib._bootstrap>", line 1022, in load_module
File "<frozen importlib._bootstrap>", line 1003, in load_module
File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
File "<frozen importlib._bootstrap>", line 868, in _load_module
File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/registration/models.py", line 28, in <module>
class RegistrationManager(models.Manager):
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/registration/models.py", line 94, in RegistrationManager
create_inactive_user = transaction.commit_on_success(create_inactive_user)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/transaction.py", line 449, in commit_on_success
warnings.warn("commit_on_success is deprecated in favor of atomic.",
PendingDeprecationWarning: commit_on_success is deprecated in favor of atomic.

I dropped into a pdb session from /home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/transaction.py(449)commit_on_success(). Stepping up two stack frames, this has been called from line 28 of site-packages/registration/models.py(28) -> class RegistrationManager(models.Manager)


Has anyone else tried to use django-registration 1.0 with Django 1.6 and seen this failure?


Here is the settings/ directory for reference: base.py:


# Django settings for EduDuck project.

import os
import django
from django.conf import global_settings

DEBUG = False

#get the path name to prepend to other settings
DJANGO_ROOT = os.path.dirname(os.path.realpath(django.__file__))

SITE_ROOT = os.path.join(os.path.dirname(__file__),
os.path.pardir,
os.path.pardir)

# User Profile model
AUTH_PROFILE_MODULE = 'bio.Bio'
LOGIN_REDIRECT_URL = '/courses/'

# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
TIME_ZONE = 'UTC'

# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-gb'

#https://docs.djangoproject.com/en/dev/ref/contrib/sites/?from=olddocs
SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media/')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/var/www/static/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files

STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'core.context_processors.git_branch_render',
'core.context_processors.show_survey_link',
)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'core.middleware.TimezoneMiddleware',
)

ROOT_URLCONF = 'EduDuck.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'EduDuck.wsgi.application'

#TODO: Ensure templates aren't under docroot for production version
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

#django-registration https://bitbucket.org/ubernostrum/django-registration
#This allows new users 7 days to activate new accounts
ACCOUNT_ACTIVATION_DAYS = 7
#Set following to False to disable registration.
REGISTRATION_OPEN = True

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',

#Temporarily disabling haystack - 'six' package/bundled conflict?
#django-haystack via elasticsearch backend
#'haystack',

#following provides account activation via email via django-registration
'registration',

#eduduck apps
'courses',
'quiz',
'support',
'bio',
'interaction',
'outcome',
'attachment',
)

# See http://django-haystack.readthedocs.org/en/latest/tutorial.html#simple
# and override this in staging.py and production.py
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
#this is only here since I can't get python manage.py to read this config (handled via wsgi)
#use via python manage.py rebuild_index --using='forcron'
'forcron': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}

# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
'standard': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
},
#TODO uncomment when on Django 1.5
# 'require_debug_true': {
# '()': 'django.utils.log.RequireDebugTrue'
# }
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'django.utils.log.NullHandler',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'log_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/tmp/eduduck.log',
'maxBytes': 10*2**20, #10 MB
'backupCount': 5,
'formatter': 'standard',
},
'log_filedb': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/tmp/eduduck_db.log',
'maxBytes': 10*2**20, #10 MB
'backupCount': 5,
'formatter': 'standard',
},
'console': {
'level': 'ERROR',
'class': 'logging.StreamHandler',
'formatter': 'standard',
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'': {
'handlers': ['log_file', 'console'],
'level': 'DEBUG',
'propagate': True,
},
'django.db.backends': {
'handlers': ['log_filedb', 'console'],
'propagate': False,
'level': 'DEBUG',
},
}
}

and dev.py


#settings/dev.py
from .base import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG
TEMPLATE_STRING_IF_INVALID = 'INVALID_EXPRESSION: %s'

#django-registration needs an MTA. For development just use console
#smtp is the default, so in prod.py, EMAIL_BACKEND is commented out or missing
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

#INSTALLED_APPS += ("debug_toolbar", )
#INTERNAL_IPS = ("127.0.0.1", )
#MIDDLEWARE_CLASSES += ("debug_toolbar.middleware.DebugToolbarMiddleware",)

DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'ed_dev',
'USER': 'ed_dev',
'PASSWORD': 'quickquackquock',
'HOST': '',
'PORT': '',
},
# following is for resyncing database on staging server (would be better to
# get python manage.py to read the staging.py config, as opposed to having this here.
# 'mysql_sync': {
# 'ENGINE': 'django.db.backends.mysql',
# 'HOST': '',
# 'NAME': 'eduduck',
# 'USER': 'put it back if you need to resync',
# 'PORT': '2369',
# 'PASSWORD': "put it back if you need to resync",
# 'OPTIONS': {
# 'init_command': 'SET storage_engine=INNODB',
# },
# },
}

#Not very secret SECRET_KEY. Just for dev. Staging and prod. use env var.
SECRET_KEY = 'paranoid'

# Fixture Directory - for development purposes, reloading test data
# after changes to models.
FIXTURE_DIRS = (
os.path.join(SITE_ROOT, 'fixtures/')
)

Thanks in advance,



I'm trying to upgrade a webapp called courses from Django 1.4 to 1.6 with django-registration 1.0. None of the other django-registration threads seem to have anything relating to this problem.


I'm running unit-tests via python manage.py test courses which crashes with the following trace:


> /home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/transaction.py(449)commit_on_success()
-> warnings.warn("commit_on_success is deprecated in favor of atomic.",
(Pdb) c
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/test/runner.py", line 145, in run_tests
old_config = self.setup_databases()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/test/runner.py", line 107, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/test/runner.py", line 279, in setup_databases
verbosity, autoclobber=not interactive)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/backends/creation.py", line 339, in create_test_db
load_initial_data=False)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/__init__.py", line 159, in call_command
return klass.execute(*args, **defaults)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 284, in execute
self.validate()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/models/loading.py", line 75, in _populate
self.load_app(app_name, True)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/models/loading.py", line 99, in load_app
models = import_module('%s.models' % app_name)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
File "<frozen importlib._bootstrap>", line 1022, in load_module
File "<frozen importlib._bootstrap>", line 1003, in load_module
File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
File "<frozen importlib._bootstrap>", line 868, in _load_module
File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/registration/models.py", line 28, in <module>
class RegistrationManager(models.Manager):
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/registration/models.py", line 94, in RegistrationManager
create_inactive_user = transaction.commit_on_success(create_inactive_user)
File "/home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/transaction.py", line 449, in commit_on_success
warnings.warn("commit_on_success is deprecated in favor of atomic.",
PendingDeprecationWarning: commit_on_success is deprecated in favor of atomic.

I dropped into a pdb session from /home/chris/.virtualenvs/blanket/lib/python3.3/site-packages/django/db/transaction.py(449)commit_on_success(). Stepping up two stack frames, this has been called from line 28 of site-packages/registration/models.py(28) -> class RegistrationManager(models.Manager)


Has anyone else tried to use django-registration 1.0 with Django 1.6 and seen this failure?


Here is the settings/ directory for reference: base.py:


# Django settings for EduDuck project.

import os
import django
from django.conf import global_settings

DEBUG = False

#get the path name to prepend to other settings
DJANGO_ROOT = os.path.dirname(os.path.realpath(django.__file__))

SITE_ROOT = os.path.join(os.path.dirname(__file__),
os.path.pardir,
os.path.pardir)

# User Profile model
AUTH_PROFILE_MODULE = 'bio.Bio'
LOGIN_REDIRECT_URL = '/courses/'

# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
TIME_ZONE = 'UTC'

# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-gb'

#https://docs.djangoproject.com/en/dev/ref/contrib/sites/?from=olddocs
SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media/')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = '/var/www/static/'

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# Additional locations of static files

STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static/'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'core.context_processors.git_branch_render',
'core.context_processors.show_survey_link',
)

MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'core.middleware.TimezoneMiddleware',
)

ROOT_URLCONF = 'EduDuck.urls'

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'EduDuck.wsgi.application'

#TODO: Ensure templates aren't under docroot for production version
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

#django-registration https://bitbucket.org/ubernostrum/django-registration
#This allows new users 7 days to activate new accounts
ACCOUNT_ACTIVATION_DAYS = 7
#Set following to False to disable registration.
REGISTRATION_OPEN = True

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',

#Temporarily disabling haystack - 'six' package/bundled conflict?
#django-haystack via elasticsearch backend
#'haystack',

#following provides account activation via email via django-registration
'registration',

#eduduck apps
'courses',
'quiz',
'support',
'bio',
'interaction',
'outcome',
'attachment',
)

# See http://django-haystack.readthedocs.org/en/latest/tutorial.html#simple
# and override this in staging.py and production.py
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
#this is only here since I can't get python manage.py to read this config (handled via wsgi)
#use via python manage.py rebuild_index --using='forcron'
'forcron': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'haystack',
},
}

# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s %(message)s'
},
'standard': {
'format' : "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
'datefmt' : "%d/%b/%Y %H:%M:%S"
},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
},
#TODO uncomment when on Django 1.5
# 'require_debug_true': {
# '()': 'django.utils.log.RequireDebugTrue'
# }
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'django.utils.log.NullHandler',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
'log_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/tmp/eduduck.log',
'maxBytes': 10*2**20, #10 MB
'backupCount': 5,
'formatter': 'standard',
},
'log_filedb': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/tmp/eduduck_db.log',
'maxBytes': 10*2**20, #10 MB
'backupCount': 5,
'formatter': 'standard',
},
'console': {
'level': 'ERROR',
'class': 'logging.StreamHandler',
'formatter': 'standard',
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'': {
'handlers': ['log_file', 'console'],
'level': 'DEBUG',
'propagate': True,
},
'django.db.backends': {
'handlers': ['log_filedb', 'console'],
'propagate': False,
'level': 'DEBUG',
},
}
}

and dev.py


#settings/dev.py
from .base import *

DEBUG = True
TEMPLATE_DEBUG = DEBUG
TEMPLATE_STRING_IF_INVALID = 'INVALID_EXPRESSION: %s'

#django-registration needs an MTA. For development just use console
#smtp is the default, so in prod.py, EMAIL_BACKEND is commented out or missing
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

#INSTALLED_APPS += ("debug_toolbar", )
#INTERNAL_IPS = ("127.0.0.1", )
#MIDDLEWARE_CLASSES += ("debug_toolbar.middleware.DebugToolbarMiddleware",)

DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'ed_dev',
'USER': 'ed_dev',
'PASSWORD': 'quickquackquock',
'HOST': '',
'PORT': '',
},
# following is for resyncing database on staging server (would be better to
# get python manage.py to read the staging.py config, as opposed to having this here.
# 'mysql_sync': {
# 'ENGINE': 'django.db.backends.mysql',
# 'HOST': '',
# 'NAME': 'eduduck',
# 'USER': 'put it back if you need to resync',
# 'PORT': '2369',
# 'PASSWORD': "put it back if you need to resync",
# 'OPTIONS': {
# 'init_command': 'SET storage_engine=INNODB',
# },
# },
}

#Not very secret SECRET_KEY. Just for dev. Staging and prod. use env var.
SECRET_KEY = 'paranoid'

# Fixture Directory - for development purposes, reloading test data
# after changes to models.
FIXTURE_DIRS = (
os.path.join(SITE_ROOT, 'fixtures/')
)

Thanks in advance,


base de données - Django raw SQL executaion - Stack Overflow


When we execute raw SQL in Django, say insert a tuple into a table. But the tuple we want to insert already exists in the table, how would django handle that? What should I do to handle that?




If you're executing raw SQL, then it's up to you to handle any integrity errors that occur, just as you would with any other pure SQL application.



When we execute raw SQL in Django, say insert a tuple into a table. But the tuple we want to insert already exists in the table, how would django handle that? What should I do to handle that?



If you're executing raw SQL, then it's up to you to handle any integrity errors that occur, just as you would with any other pure SQL application.


python - Django, RabbitMQ, & céleri - pourquoi céleri court les anciennes versions de mes tâches après que j'ai mis à jour mon code de Django en développement ? -Débordement de pile


So I have a Django app that occasionally sends a task to Celery for asynchronous execution. I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development. If I change code that will later be run in a Celery task, Celery will still keep running the old version of the code. The only way I can get it to pick up on the change is to:



  1. stop the Celery worker

  2. stop RabbitMQ

  3. reset RabbitMQ

  4. start RabbitMQ

  5. add the user to RabbitMQ that my Django app is configured to use

  6. set appropriate permissions for this user

  7. restart the Celery worker


This seems like a far more drastic approach than I should have to take, however. Is there a more lightweight approach I can use?





I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development.



What you've described here is exactly correct and expected. Keep in mind that Python will use a module cache, so you WILL need to restart the Python interpreter before you can use the new code.


The question is "Why doesn't Celery pick up the new version", but this is how most libraries will work. The Django development server, however, is an exception. It has special code that helps it automatically reload Python code as necessary. It basically restarts the web server without you needing to restart the web server.


Note that when you run Django in development, you probably WILL have to restart your server (since you won't be using the development server in production, and most production servers don't try to take on the hassle of implementing a problematic feature of detecting file changes and auto-reloading the server).


Finally, you shouldn't need to restart RabbitMQ. You should only have to restart the Celery worker to use the new version of the Python code. You might have to clear the queue if the new version of the code is changing the data in the message, however. For example, the Celery worker might be receiving version 1 of the message when it is expecting to receive version 2.



So I have a Django app that occasionally sends a task to Celery for asynchronous execution. I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development. If I change code that will later be run in a Celery task, Celery will still keep running the old version of the code. The only way I can get it to pick up on the change is to:



  1. stop the Celery worker

  2. stop RabbitMQ

  3. reset RabbitMQ

  4. start RabbitMQ

  5. add the user to RabbitMQ that my Django app is configured to use

  6. set appropriate permissions for this user

  7. restart the Celery worker


This seems like a far more drastic approach than I should have to take, however. Is there a more lightweight approach I can use?




I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development.



What you've described here is exactly correct and expected. Keep in mind that Python will use a module cache, so you WILL need to restart the Python interpreter before you can use the new code.


The question is "Why doesn't Celery pick up the new version", but this is how most libraries will work. The Django development server, however, is an exception. It has special code that helps it automatically reload Python code as necessary. It basically restarts the web server without you needing to restart the web server.


Note that when you run Django in development, you probably WILL have to restart your server (since you won't be using the development server in production, and most production servers don't try to take on the hassle of implementing a problematic feature of detecting file changes and auto-reloading the server).


Finally, you shouldn't need to restart RabbitMQ. You should only have to restart the Celery worker to use the new version of the Python code. You might have to clear the queue if the new version of the code is changing the data in the message, however. For example, the Celery worker might be receiving version 1 of the message when it is expecting to receive version 2.