开发者

Model Manager filtering on Boolean causing IntegrityError

开发者 https://www.devze.com 2023-02-03 08:53 出处:网络
I am using a Manager on a model based on a Boolean field to filter the objects displayed on the site while showing all objects in the admin unfiltered. The idea is that user\'s are submitting Location

I am using a Manager on a model based on a Boolean field to filter the objects displayed on the site while showing all objects in the admin unfiltered. The idea is that user's are submitting Locations but I do not want them to show on the site until they have been verified as a valid location based on my criteria.

models.py

class LocationManager(models.GeoManager):
    def get_query_set(self):
        return super(LocationManager, self).get_query_set().filter(verified=True)

class Location(models.Model):
    verified = models.BooleanField(default=False)
    objects = LocationManager()
    admin_objects = models.Manager()

admin.py

class LocationAdmin(admin.OSMGeoAdmin):
    def queryset(self, request):
        qs = self.model.admin_objects.get_query_set()
   开发者_如何学Go     return qs

admin.site.register(Location, LocationAdmin)

In the admin, when I go into a record and check the verified Boolean to True and press save, I get an IntegrityError:

duplicate key value violates unique constraint "localshare_location_pkey"

This worked on another project when default was True and I filtered for False. I am using Postgres. Does anyone know why this is not working or have suggestions for a better way to achieve this?


For anyone interested, this is the answer provided by the django IRC channel. The admin looks for the first Manager by default. All I had to do was flip the order they showed up in the Model. Even with the admin.py overriding queryset and pointing to the other Manager, the order is important.

fixed models.py

class Location(models.Model):
    verified = models.BooleanField(default=False)
    admin_objects = models.Manager()
    objects = LocationManager()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号