模型管理器对布尔值进行过滤导致 IntegrityError
我在基于布尔字段的模型上使用管理器来过滤网站上显示的对象,同时显示管理中未过滤的所有对象。这个想法是用户正在提交位置,但我不希望它们显示在网站上,直到根据我的标准将它们验证为有效位置。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何感兴趣的人,这是 django IRC 频道提供的答案。管理员默认查找第一个经理。我所要做的就是翻转它们在模型中出现的顺序。即使 admin.py 覆盖查询集并指向另一个管理器,顺序也很重要。
固定模型.py
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