How to fix Django warning “(models.W042) Auto-created primary key used when not defining a primary key type”?

Your models do not have primary keys. But they are being created automatically by django. You need to choose the type of auto-created primary keys: https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys (new in Django 3.2) Either add this to settings.py: DEFAULT_AUTO_FIELD=’django.db.models.AutoField’ or class Topic(models.Model): id = models.AutoField(primary_key=True) …

Django unique, null and blank CharField giving ‘already exists’ error on Admin page

None of the answers clearly describe the root of the problem. Normally in the db you can make a field null=True, unique=True and it will work… because NULL != NULL. So each blank value is still considered unique. But unfortunately for CharFields Django will save an empty string “” (because when you submit a form … Read more

How to get two random records with Django

The order_by(‘?’)[:2] solution suggested by other answers is actually an extraordinarily bad thing to do for tables that have large numbers of rows. It results in an ORDER BY RAND() SQL query. As an example, here’s how mysql handles that (the situation is not much different for other databases). Imagine your table has one billion … Read more

Django: When extending User, better to use OneToOneField(User) or ForeignKey(User, unique=True)?

The only real reason given in the article is that it can be set up so that the admin page for User will show both the fields in User and UserProfile. This can be replicated with a OneToOneField with a little elbow grease, so unless you’re addicted to showing it in the admin page with … 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 Left Outer Join

First of all, there is no a way (atm Django 1.9.7) to have a representation with Django’s ORM of the raw query you posted, exactly as you want; however, you can get the same desired result with something like: >>> Topic.objects.annotate( f=Case( When( record__user=johnny, then=F(‘record__value’) ), output_field=IntegerField() ) ).order_by( ‘id’, ‘name’, ‘f’ ).distinct( ‘id’, ‘name’ … Read more

What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django?

If you use the save method right now, Django will try to save using the manager (which Django doesn’t allow). Unfortunately, the behavior you want is a little bit trickier than what ModelForm does by default. What you need to do is create a formset. First of all, you will need to change the options … Read more

Django Manager Chaining

See this snippet on Djangosnippets: http://djangosnippets.org/snippets/734/ Instead of putting your custom methods in a manager, you subclass the queryset itself. It’s very easy and works perfectly. The only issue I’ve had is with model inheritance, you always have to define the manager in model subclasses (just: “objects = QuerySetManager()” in the subclass), even though they … Read more

tech