1

I have a Model in Django. It has a Group and inside the group are Cameras.

class Groups(models.Model):
    groupName = models.CharField(max_length=255)

class Cameras(models.Model):
    group = models.ForeignKey(Groups)
    cameraID = models.IntegerField()

When I try to remove a Camera or set of cameras in the model.

camera = Cameras.objects.filter(cameraID=int(camID))
camera.delete()

It removes any camera with an ID Greater than 0. But If I have a camera ID of '0' It fails to remove. Any ideas why this would be.

4
  • Which database do you use? Did you try to look at the sql used by the queryset? django-debugbar may help in this case Commented Jan 26, 2012 at 9:16
  • 1
    This is django-debug-toolbar, it may help. Commented Jan 26, 2012 at 9:23
  • Is there other code around this which is preventing those two lines from executing? What happens if you try to do Cameras.objects.filter(cameraID=0).delete() from a shell? Commented Jan 26, 2012 at 17:13
  • I resolved the issue. Aparantly python is annoying and if you pass a 0 as a value it assumes its empty. I was using If not x: and if x was equal to zero it thought it was wrong Commented Jan 30, 2012 at 11:35

2 Answers 2

3

When you are using filter, you can get 0 or multiple objects returned, unlike get which will only return 1 object, or cause errors.

If you want to delete every objects which is returned from a filter, you can iterate over it like so:

cameras = Cameras.objects.filter(cameraID=int(camID))
## let's say this returns [<Camera:1>,<Camera:2>]

for camera in cameras:
    camera.delete()
    ## this loop will delete each object in the query
Sign up to request clarification or add additional context in comments.

4 Comments

No, filter returns a QuerySet object, and you don't need to iterate over that. QuerySets have a .delete() method as well: docs.djangoproject.com/en/1.2/ref/models/querysets/#delete
I just realized that after I answered, though I've never actually used it. As it says in the docs, the queryset delete method does not call any special delete methods, not that the OP has any (listed). I'd rather have an extra line of code that I'd never have to worry about changing later if I added unique delete methods
I resolved the issue. Aparantly python is annoying and if you pass a 0 as a value it assumes its empty. I was using If not x: and if x was equal to zero it thought it was wrong.
@Deano glad you figured it out. Now that you say that, I remember I've had similar issues. Sometimes you just have to use if x != None: instead of if not x:. Lesson learned.
2

Try use camera = Cameras.objects.get(pk=camID) then you know for sure you've only got one object. You can then camera.delete() You also don't need all the ';' characters in your code.

1 Comment

Based on the current example code the pk for the Camera is not the cameraID, just FYI. Agree with the ';' comment. Cleaning up the example now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.