django-batch-select icon indicating copy to clipboard operation
django-batch-select copied to clipboard

BatchField, for automatically pulling in reverse relationships at the model level

Open AndrewIngram opened this issue 13 years ago • 1 comments

This library looks pretty great, one additional thing I'd look for is the ability to define batches on the models themselves. The syntax would be pretty similar to the Batch syntax on the queryset:

Post(models.Model)
    title = models.CharField(max_length=255)
    slug = models.SlugField
    body = models.TextField
    visible_tags = BatchField('tags',visible=True)

Tag (models.Model):
    post = models.ForeignKey(Post, related_name='tags')
    visible = models.BooleanField(default=True)

I'm not sure if this is feasible, but it seems like a good way of enforcing that you always want certain relationships to be eagerly-loaded. The issue with using the custom Manager is that someone could subclass my models and replace the manager and lose the batch functionality (I'm working on a library which is designed to have its models subclassed heavily).

AndrewIngram avatar Jun 02 '11 14:06 AndrewIngram

I'm not sure how feasible this will be to do - at lest based on how batch select works at the moment. The 2nd query to pull in the extra models occurs when the queryset is evaluated, so there's no easy way to get to that point from a model definition.

It might be possible to do something with contribute_to_class to ensure that there was a batch manager defined on the class and trigger an error/warning if not. The other option would be to see if it's possible to delay the 2nd query until the BatchField was accessed, which would be kind of nice. Again though without overriding the manager/queryset that's probably not possible either.

lilspikey avatar Jun 05 '11 13:06 lilspikey