Django search fields in multiple models

One solution is to query all the models # Look up Q objects for combining different fields in a single query from django.db.models import Q people = Person.objects.filter(Q(first_name__contains=query) | Q(last_name__contains=query) restaurants = Restaurant.objects.filter(restaurant_name__contains=query) pizzas = Pizza.objects.filter(pizza_name__contains=query) Then combine the results, if you want from itertools import chain results = chain(people, restaurants, pizzas) Ok, sure, here’s … Read more

SQLAlchemy Model Django like Save Method?

You can extend your models with some simple crud methods to achieve something similar to Django ORM / ActiveRecord: # SQLAlchemy db_session setup omitted … Model = declarative_base(name=”Model”) Model.query = db_session.query_property() class CRUD(): def save(self): if self.id == None: db_session.add(self) return db_session.commit() def destroy(self): db_session.delete(self) return db_session.commit() class User(Model, CRUD): __tablename__ = ‘users’ id = … Read more

Django uploads: Discard uploaded duplicates, use existing file (md5 based check)

Thanks to alTus answer, I was able to figure out that writing a custom storage class is the key, and it was easier than expected. I just omit calling the superclasses _save method to write the file if it is already there and I just return the name. I overwrite get_available_name, to avoid getting numbers … Read more

How do I refresh the values on an object in Django?

Finally, in Django 1.8, we have a specific method to do this. It’s called refresh_from_db and it’s a new method of the class django.db.models.Model. An example of usage: def update_result(self): obj = MyModel.objects.create(val=1) MyModel.objects.filter(pk=obj.pk).update(val=F(‘val’) + 1) # At this point obj.val is still 1, but the value in the database # was updated to 2. … Read more

Django Queryset only() and defer()

These methods are mostly of use when optimizing performance of your application. Generally speaking, if you are not having performance problems, you don’t need to optimize. And if you don’t need to optimize, you don’t need these functions. This is a case with a lot of advanced QuerySet features, such as select_related or prefetch_related. As … Read more

Unique model field in Django and case sensitivity (postgres)

You could define a custom model field derived from models.CharField. This field could check for duplicate values, ignoring the case. Custom fields documentation is here http://docs.djangoproject.com/en/dev/howto/custom-model-fields/ Look at http://code.djangoproject.com/browser/django/trunk/django/db/models/fields/files.py for an example of how to create a custom field by subclassing an existing field. You could use the citext module of PostgreSQL https://www.postgresql.org/docs/current/static/citext.html If you … Read more

Setting up a foreign key to an abstract base class with Django

A generic relation seems to be the solution. But it will complicate things even further. It seems to me; your model structure is already more complex than necessary. I would simply merge all three Answer models into one. This way: Answer_Risk would work without modification. You can set resident to None (NULL) in case of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)