Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "proper" way to convert django.db.models.query.ValuesListQuerySet to a pure List?

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.

like image 288
DanH Avatar asked Jan 02 '13 03:01

DanH


2 Answers

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))
like image 162
DanH Avatar answered Oct 05 '22 17:10

DanH


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)
like image 36
drevni Avatar answered Oct 05 '22 17:10

drevni