Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Model Manager filtering on Boolean causing IntegrityError [RSS Feed]

#1 Jan. 9, 2011 18:40:23

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Model Manager filtering on Boolean causing IntegrityError


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"
traceback:http://dpaste.com/299829/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?

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 9, 2011 20:15:23

Daniel R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Model Manager filtering on Boolean causing IntegrityError


On Sunday, January 9, 2011 6:40:17 PM UTC, Shantp wrote:
>
> 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"
> traceback:http://dpaste.com/299829/>
> 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?


This doesn't have anything to do with filtering on the boolean. As you
state, this happens when you *save* - integrity errors are raised by the
database when you try and update a row with data that violates one or more
constraints. In your case, you seem to have a unique constraint in the
Location model - did you previously have a unique=True on the boolean field?
If so, you'll need to remove it from the db manually.
--
DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 9, 2011 21:14:42

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Model Manager filtering on Boolean causing IntegrityError


The reason I mentioned the filtering is if I turn the filter off or
filter for only objects where the "verified" Boolean is False instead
of True, there is no unique integrity error. I did not have a
unique=True attribute in the model at any time and this is happening
on a fresh db. I'm pretty sure it has something to do the objects
being inactive and unverified and this is causing some incrementing
issue with the Postgres db. The admin is trying to create a new object
with the same id instea dof overwriting. I am quite new to Django and
SQL so I don't understand this enough to know exactly why it is
happening.

On Jan 9, 12:15 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Sunday, January 9, 2011 6:40:17 PM UTC, Shantp wrote:
>
> > 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"
> > traceback:http://dpaste.com/299829/>
> > 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?
>
> This doesn't have anything to do with filtering on the boolean. As you
> state, this happens when you *save* - integrity errors are raised by the
> database when you try and update a row with data that violates one or more
> constraints. In your case, you seem to have a unique constraint in the
> Location model - did you previously have a unique=True on the boolean field?
> If so, you'll need to remove it from the db manually.
> --
> DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 9, 2011 21:50:14

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Model Manager filtering on Boolean causing IntegrityError


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

On Jan 9, 1:14 pm, Shantp <sha...@gmail.com> wrote:
> The reason I mentioned the filtering is if I turn the filter off or
> filter for only objects where the "verified" Boolean is False instead
> of True, there is no unique integrity error. I did not have a
> unique=True attribute in the model at any time and this is happening
> on a fresh db. I'm pretty sure it has something to do the objects
> being inactive and unverified and this is causing some incrementing
> issue with the Postgres db. The admin is trying to create a new object
> with the same id instea dof overwriting. I am quite new to Django and
> SQL so I don't understand this enough to know exactly why it is
> happening.
>
> On Jan 9, 12:15 pm, Daniel Roseman <dan...@roseman.org.uk> wrote:
>
>
>
>
>
>
>
> > On Sunday, January 9, 2011 6:40:17 PM UTC, Shantp wrote:
>
> > > 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"
> > > traceback:http://dpaste.com/299829/>
> > > 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?
>
> > This doesn't have anything to do with filtering on the boolean. As you
> > state, this happens when you *save* - integrity errors are raised by the
> > database when you try and update a row with data that violates one or more
> > constraints. In your case, you seem to have a unique constraint in the
> > Location model - did you previously have a unique=True on the boolean field?
> > If so, you'll need to remove it from the db manually.
> > --
> > DR.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Model Manager filtering on Boolean causing IntegrityError [RSS Feed]

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support