Code Monkey home page Code Monkey logo

Comments (5)

jackton1 avatar jackton1 commented on June 2, 2024 1

Personally switching from using the MultipleModelField -> forms.ModelMultipleChoiceField for queryset.

my_models = forms.ModelMultipleChoiceField(queryset=MyModel.objects.all())

OR

recipients = forms.MultipleChoiceField(choices=[...])

from django-service-objects.

peterfarrell avatar peterfarrell commented on June 2, 2024

@jackton1 Thoughts? It would smooth out using the MultipleModelField. Right now you have to coerce all input to a MultipleModelField into a list before sending it in as input to the service. I'd be willing to put in a PR for this -- just don't want to waste my time if it won't be accepted.

from django-service-objects.

peterfarrell avatar peterfarrell commented on June 2, 2024

Thanks for the idea @jackton1. forms.ModelMultipleChoiceField would expect the input to be a list of PKs so you couldn't pass it a queryset or list of objects. In my use case, we already have a queryset to pass into the service so it would not be performant to convert a queryset into a list of PK only for forms.ModelMultipleChoiceField to transform those PKs back into a queryset.

This is why I was proposing that service_objects.fields.ModelMultipleChoiceField should accept any reasonable iterable containing model objects like queryset, list, set, or tuple -- not just a list.

from django-service-objects.

jackton1 avatar jackton1 commented on June 2, 2024

Yeah I can see an argument for switching to

from collections import Iterable

...
        if not isinstance(values, Iterable):
            raise ValidationError(self.error_non_list)

Might have to rename error_non_list -> error_non_iterable

Question:
For queryset's ensuring that allow_unsaved is always set to False since this is pulling a list of objects from the db.


if isinstance(values, QuerySet) and self.allow_unsaved:
   raise ValidationError('...')

from django-service-objects.

peterfarrell avatar peterfarrell commented on June 2, 2024

@jackton1 Thanks for the feedback and mentioning the allow_unsaved. I guess @c17r already committed a change so I won't submit the PR I started last night. Glad to see this improvement.

from django-service-objects.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.