I'm attempted to delete records from a number of models in a single method, were I have the following schema:
picture 1:1 picture_foreign_picture *:1 picture_foreign
I'm deleting these given list picture_foreign objects:
picture_foreign_pictures = PictureForeignPicture.objects.filter(picture_foreign__in=picture_foreigns)
picture_ids = picture_foreign_pictures.values_list('picture_id', flat=True)
logger.warn('PICTURES REMOVE: %s' % picture_ids)
picture_foreign_pictures.delete()
logger.warn('PICTURES REMOVE: %s' % picture_ids)
The 2 loggers lines output the following:
WARNING 2013-01-02 03:40:10,974 PICTURES REMOVE: [86L]
WARNING 2013-01-02 03:40:11,045 PICTURES REMOVE: []
Despite this however, the picture 86
still exists:
mysql> select id from picture where id = 86;
+----+
| id |
+----+
| 86 |
+----+
1 row in set (0.00 sec)
I guess I could get around this by simply converting picture_ids
into a pure integer list however I'm wondering whether there's a more Django method to this? I would have thought flat=True
would already handle this but it seems to be more than just a pure list.
Well, I'm not sure it's proper, but it's ridiculously simple to use list()
to accomplish this:
picture_ids = list(picture_foreign_pictures.values_list('picture_id', flat=True))
Above solution is not working:
You can convert to pur list like this:
p_ids =PictureForeignPicture.objects.filter(picture_foreign__in=picture_foreigns).values_list('picture_id', flat=True)
new_list = [];new_list.extend(p_ids)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With