模型管理器对布尔值进行过滤导致 IntegrityError

发布于 2024-10-11 03:54:48 字数 964 浏览 3 评论 0原文

我在基于布尔字段的模型上使用管理器来过滤网站上显示的对象,同时显示管理中未过滤的所有对象。这个想法是用户正在提交位置,但我不希望它们显示在网站上,直到根据我的标准将它们验证为有效位置。

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()
        return qs

admin.site.register(Location, LocationAdmin)

在管理中,当我进入记录并将验证的布尔值检查为 True 并按保存时,我收到一个 IntegrityError:

duplicate key value violates unique constraint "localshare_location_pkey"

这适用于另一个项目时默认为 True,我过滤为 False。我正在使用 Postgres。有谁知道为什么这不起作用或有更好的方法来实现这一目标的建议?

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()
        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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳哥哥 2024-10-18 03:54:48

对于任何感兴趣的人,这是 django IRC 频道提供的答案。管理员默认查找第一个经理。我所要做的就是翻转它们在模型中出现的顺序。即使 admin.py 覆盖查询集并指向另一个管理器,顺序也很重要。

固定模型.py

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文