Creating Partial Indexes with Django 1.7

Django 2.2 and later As of version 2.2 Django supports declarative partial unique indexes on databases that support them (PostgreSQL and SQLite). So you could do something like: from django.db.models import Model, Q, UniqueConstraint class Post(Model): … class Meta: constraints = [ UniqueConstraint( fields=[“title”, “blog”, “category”], name=”idx1″, condition=Q(category__isnull=False)), UniqueConstraint( fields=[“title”, “blog”], name=”idx2″, condition=Q(category__isnull=True)), ] Django … Read more

Django filter JSONField list of dicts

If you wan’t to filter your data by one of fields in your array of dicts, you can try this query: Test.objects.filter(actions__contains=[{‘fixed_key_1’: ‘foo2’}]) It will list all Test objects that have at least one object in actions field that contains key fixed_key_1 of value foo2. Also it should work for nested lookup, even if you … Read more

Django foreign key relation in template

If you review the foreign key documentation, if you have a relationship like Doc -> has many DocImages you need to define your foreign key on the DocImages class like so: class DocImage(models.Model): property = models.ForeignKey(Doc, related_name=”images”) If you don’t set related names, you can access the DocImages from the Doc like: Doc.docimage_set.all() Docs on … Read more

How to validate uniqueness constraint across foreign key (django)

Methods are not called on their own when saving the model. One way to do this is to have a custom save method that calls the validate_unique method when a model is saved: class Room(models.Model): zone = models.ForeignKey(Zone) name = models.CharField(max_length=255) def validate_unique(self, exclude=None): qs = Room.objects.filter(name=self.name) if qs.filter(zone__site=self.zone__site).exists(): raise ValidationError(‘Name must be unique per … Read more

Django 2.1.3 Error: __init__() takes 1 positional argument but 2 were given

You need use as_view() at the end of class based views when declaring in the urls: path(”, views.HomeView.as_view(), name=”homepage”), Also, when using login_required decorator, you need to use it on dispatch method of CBV: from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator class HomeView(ListView): @method_decorator(login_required) def dispatch(self, *args, **kwargs): return super(HomeView, self).dispatch(*args, **kwargs)

Password field in Django model

As @mlissner suggested the auth.User model is a good place to look. If you check the source code you’ll see that the password field is a CharField. password = models.CharField(_(‘password’), max_length=128, help_text=_(“Use ‘[algo]$[salt]$[hexdigest]’ or use the <a href=\”password/\”>change password form</a>.”)) The User model also has a set_password method. def set_password(self, raw_password): import random algo = … Read more

Sharing models between Django apps

The answer is yes. It’s perfectly okay for one application inside your django project to import models from another application. The power of a django project lies in the apps and their interactions. Also make sure that you have utility applications importing models from more generic applications and not the other way. So “userstatistics” app … Read more