- if value is None:
- self.object_ids = ""
- return
- if not isinstance(value, models.query.QuerySet):
- raise TypeError("Value must be a QuerySet.")
- self.content_type = ContentType.objects.get_for_model(value.model)
- self.object_ids = ','.join([`value` for value in value.values_list('id', flat=True)])
+ # Value is probably a queryset - but allow any iterable.
+
+ # These lines shouldn't be necessary; however, if value is an EmptyQuerySet,
+ # the code won't work without them. Unclear why...
+ if not value:
+ value = []
+
+ if isinstance(value, models.query.QuerySet):
+ value = value.values_list('id', flat=True)
+
+ self.values.filter(~models.Q(object_id__in=value)).delete()
+ current = self.get_object_id_list()
+
+ for v in value:
+ if v in current:
+ continue
+ self.values.create(content_type=self.content_type, object_id=v)