django-orm case-insensitive order by
Since Django 1.8 it is possible with: from django.db.models.functions import Lower MyModel.objects.order_by(Lower(‘myfield’)) https://code.djangoproject.com/ticket/6498
Since Django 1.8 it is possible with: from django.db.models.functions import Lower MyModel.objects.order_by(Lower(‘myfield’)) https://code.djangoproject.com/ticket/6498
From version 1.6 of Django, the test discovery mechanism changed. You no longer need to import everything from tests/__init__.py, but now you have to make sure that all your files containing tests match the pattern test*.py. So, instead of having app/tests/models.py and app/tests/views.py, now you should have app/tests/test_models.py and app/tests/test_views.py. You could also create a … Read more
In the view, you can access the URL args and kwargs as self.args and self.kwargs. class MyView(View): def my_method(self): this_name = self.kwargs[‘this_name’] If you only want to access the value in the template, then you don’t need to make any changes in the view. The base get_context_data method adds the view to the context as … Read more
The form_valid() method for CreateView and UpdateView saves the form, then redirects to the success url. It’s not possible to do return super(), because you want to do stuff in between the object being saved and the redirect. The first option is to not call super(), and duplicate the two lines in your view. The … Read more
Here is a fully coded answer. The idea of the solution is: you have to first visit the login page with GET to get the cookies file generated, then parse the CSRF token out of the cookies file and do the login using a POST request, passing the data with -d. Afterwards you can perform … Read more
This is now possible on Django 2.2 Django 2.2 adds a new ignore_conflicts option to the bulk_create method, from the documentation: On databases that support it (all except PostgreSQL < 9.5 and Oracle), setting the ignore_conflicts parameter to True tells the database to ignore failure to insert any rows that fail constraints such as duplicate … Read more
for f in request.FILES.getlist(‘file’): # do something with the file f… EDIT: I know this was an old answer, but I came across it just now and have edited the answer to actually be correct. It was previously suggesting that you could iterate directly over request.FILES[‘file’]. To access all items in a MultiValueDict, you use … Read more
If you’re sure there are only integers in the field, you could get the database to cast it as an integer via the extra method, and order by that: MyModel.objects.extra( select={‘myinteger’: ‘CAST(mycharfield AS INTEGER)’} ).order_by(‘myinteger’)
In your admin definition, you can define a queryset() method that returns the queryset for that model’s admin. eg: class MyModelAdmin(admin.ModelAdmin): def queryset(self, request): qs = super(MyModelAdmin, self).queryset(request) return qs.filter(user=request.user) Then only objects with user=request.user will be visible in the admin.
If you set your session cookie domain to start with a “.” character it will let you handle wildcard sub-domains and share a session cookie (login session) across multiple subdomains. In settings.py: SESSION_COOKIE_DOMAIN=”.stackoverflow.com” The above would allow a cookie to be shared across user1.stackoverflow.com and user2.stackoverflow.com. If you really do want the url’s to be … Read more