DJR
DJR

Reputation: 21

DJANGO ModelAdmin SQLLite error : DISTINCT ON fields is not supported by this database backend

My model is

class Contrat(models.Model):
    Procedure = models.ForeignKey(Procedure)
    Contrat_text = models.CharField(max_length=150)

    def __str__(self):
        return self.Contrat_text

Class Document(models.Model):
    Contrat = Contrat.objects.order_by('Contrat_text').distinct('Contrat_text')
    isContrat = models.BooleanField(('Contrat'), default=True)
    isCdC = models.BooleanField(('Cahier des Charges'), default=False)
    isCR = models.BooleanField(('Cahier de Recette'), default=False)

    def __str__(self):
        return self.Contrat

and my admin model is:

class DocumentAdmin(admin.ModelAdmin):
        fields = ('Contrat', 'isContrat', 'isCdC', 'isCR')
        list_display = ('id','isContrat', 'isCdC', 'isCR')
        ordering = ('id',)

    admin.site.register(Document, DocumentAdmin)

but what I get is:

DISTINCT ON fields is not supported by this database backend

Somebody know how to create a model admin without error?

Upvotes: 1

Views: 6060

Answers (2)

user1377396
user1377396

Reputation:

Your problem is with this line

Contrat = Contrat.objects.order_by('Contrat_text').distinct('Contrat_text')

.distinct() can't be used with sqlite3 so you will have to switch to something like postgres to use it or modify your query. Also Each field in your model should be an instance of the appropriate Field class. so you are incorrectly defining your Contrat model field.

Upvotes: 2

jay_x
jay_x

Reputation: 11

For mysql: you can use Model.objects.values_list('field').order_by('field').distinct()

Upvotes: 1

Related Questions