django-orm

Can't set character_set_results to latin1

偶尔善良 提交于 2020-01-05 03:03:43
问题 I've decided to use Django 1.7 for the first time with Python 3. I need to be able to work with legacy latin1 database which contains utf8 data. I know it sucks, but the database is so huge that it's really impossible to change this. So I tried following: DATABASES = { 'ENGINE' : 'django.db.backends.mysql', // using MySQL-python fork with support for py3 ... 'OPTIONS' : { 'init_command': "SET character_set_results = 'latin1'", #'read_default_file': '/etc/my.cnf.d/client.cnf', // I've also

Django: Perform case-insensitive lookups by default

不羁岁月 提交于 2020-01-03 09:23:20
问题 I need to perform case-insensitive queries on username by default when using the Django Auth framework. I tried fixing the issue by writing a custom subclass of Queryset and overriding the _filter_or_exclude method and then using that subclass in a custom manager for the User model- from django.db.models import Manager from django.db.models.query import QuerySet from django.contrib.auth.models import UserManager class MyQuerySet(QuerySet): def _filter_or_exclude(self, negate, *args, **kwargs)

Django ORM: See if a model has no foreign key entry in another model

孤街浪徒 提交于 2020-01-03 08:29:49
问题 So , I have this 2 models: class Site(models.Model): ... ... and another one: class SiteInfo(models.Model): ... ... site = models.ForeignKey(Site) Is there a way to get the Sites that have no entry in the SiteInfo ? 回答1: Site.objects.filter(siteinfo__isnull=True) 回答2: There is a generic way to find the list of all reverse relations of a model. reverse_model_array = [f.related_model for f in model._meta.get_fields() if f.auto_created and not f.concrete] This will list all the models that have

Django - Custom virtual model field with companion stored field [closed]

你。 提交于 2020-01-03 05:12:08
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 10 years ago . I would like to have a ConfirmationField field type. I want this field to work like a boolean field. I don't need to store this information on database instead I would like to store the date of confirmation on a seperate field. class MyModel(models.Model): confirmation = ConfirmationField() m = MyModel() m

Is there a library to benchmark my Django App for SQL requests?

你离开我真会死。 提交于 2020-01-03 02:52:10
问题 I have a large complex Django App that we use that does lots of things. From looking at the Django Debug Toolbar some of our views do a lot of SQL requests. I want to increase the performance of it by reducing the number of SQL requests (e.g. by adding more select_related , more clever queries, etc.). I would like to be able to measure the improvement as I go along, to encourage me and to be able to say how much fat has been trimmed. I have a large set of django unit tests for this app that

ManyToMany field not saved when using Django admin

心不动则不痛 提交于 2020-01-02 01:02:10
问题 I'm experiencing a weird problem which I hope someone in here may be able to shed some light on. I'm overriding the save() method of a model to add some values to a ManyToMany-field after running super(). My problem is that when I'm saving in Django admin the values seems to get added to the relationship but is then empty again. If however I do it from manage.py shell it works without problem. I've put two print statements in there and they produce the exact same output regardless of if I'm

Primary key requirement in raw SQL complicates the query in Django

ε祈祈猫儿з 提交于 2020-01-01 09:39:18
问题 To get max value from a simple table of values, I can write the following query in Django: MyTable.objects.aggregate(Max('value')) The SQL generated is : 'SELECT MAX("mytable"."value") AS "value__max" FROM "mytable"' Now if I write the same SQL using the raw query manager: 1. MyTable.objects.raw('SELECT max(value) FROM mytable') Django throws an error InvalidQuery: Raw query must include the primary key . This is also mentioned in Django docs: "There is only one field that you can’t leave out

Django order items by two fields, but ignoring them if they're zero

牧云@^-^@ 提交于 2020-01-01 08:05:18
问题 I have the following model (greatly simplified for the purposes of this question): class Product(models.Model): price = models.DecimalField(max_digits=8, decimal_places=2) sale_price = models.DecimalField(max_digits=10, blank=True, null=True, decimal_places=2) For the majority of products, price will be filled but sale_price will not be. So, I can order products by price like so: Product.objects.order_by('price') Product.objects.order_by('-price') However, some products will have a sale_price

Django N+1 query solution

旧城冷巷雨未停 提交于 2020-01-01 08:02:19
问题 I visited http://guides.rubyonrails.org/active_record_querying.html after talking with a peer regarding N+1 and the serious performance implications of bad DB queries. ActiveRecord (Rails): clients = Client.includes(:address).limit(10) Where client's have addresses, and I intend to access them while looping through the clients, Rails provides includes to let it know to go ahead and add them to the query, which eliminates 9 queries right off the bat. Django: https://github.com/lilspikey/django

Changing South Migration Directory

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 04:46:06
问题 How do you change the location where South looks for an app's migrations? By default, South assumes an app's migrations are in /migrations. However, I've migrated the model of a third-party package which is installed at /usr/local/lib/python-2.6/dist-packages/, so South is looking for migrations there, instead of the location of my custom codebase. 回答1: In settings.py: SOUTH_MIGRATION_MODULES = { 'books': 'myproject.app_name.migrations', } 来源: https://stackoverflow.com/questions/5994420