Open kwgtpat opened 6 years ago
I suppose you would have to save the order in the view, and then make them use it to display items.
Le mar. 8 mai 2018 à 11:48, nau notifications@github.com a écrit :
I'd like to update many-to-many through model data using Django-autocomplete-light's ModelSelect2Multiple, but when sending data to the form with theget_initial(), the data is not displayed in the order of the data sent; it will be displayed in the database registration order.
How should I do to display data in ModelSelect2Multiple in the sorted order in through model?
ModelSelect2Multiple works well when new data is registered.
model.py
class Writer(models.Model): name = models.CharField()
class Event(models.Model): name = models.CharField() writer = models.ManyToManyField(Writer, through="Attend", related_name='attends')
class Attend(models.Model): event = models.ForeignKey(Event) writer = models.ForeignKey(Writer) number = models.PositiveSmallIntegerField(default=0)
preview.py
class EventUpdateFormPreview(FormPreview): form_template = "app/event_update.html" preview_template = "app/event_update_preview.html"
def get_initial(self, request): event = Event.objects.get(id=self.state["event_id"]) return { 'name': event.name, 'writer': [x.pk for x in event.writer.all().order_by('attend__number')], }
forms.py
class EventUpdateForm(forms.ModelForm): name = forms.CharField(label='event name') writer = forms.ModelMultipleChoiceField( label='Attends', queryset=Writer.objects.all(), widget=autocomplete.ModelSelect2Multiple( url='app:writer_autocomplete', ) )
Registration order in datababase
Writer Mr.A(pk=1) Writer Mr.B(pk=2) Writer Mr.C(pk=3) Writer Mr.D(pk=4) The order of attendees in registered events(It will be successful at the initial registration)
Writer Mr.D(pk=4) Writer Mr.A(pk=1) Writer Mr.C(pk=3) The order displayed on the update view
Writer Mr.A(pk=1) Writer Mr.C(pk=3) Writer Mr.D(pk=4)
Environment: Django==1.11.6 django-autocomplete-light==3.2.10
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/yourlabs/django-autocomplete-light/issues/1013, or mute the thread https://github.com/notifications/unsubscribe-auth/AAFxrGWRKdIHgXpFwKyLlrp69vfNdu9Tks5twWntgaJpZM4T2Vyw .
My understanding is that the order is already stored in Attentd model number... It would be greatly appreciated if you could explain the details.
Did you ever find the right answer to this? I'd run into the same problem recently and—at the risk of invoking Cunningham's Law—here's what worked for me, as per your example:
class EventUpdateForm(forms.ModelForm):
# everything as above
def __init__(*args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["writer"].widget.choices.queryset = (
self.fields["writer"]
.widget.choices.queryset.filter(attend__event_id=self.instance.id)
.order_by("attend__number")
)
Essentially: overwriting the __init__()
function to further filter and order the choices.queryset
of the widget in question.
I'd like to update many-to-many through model data using Django-autocomplete-light's
ModelSelect2Multiple
, but when sending data to the form with theget_initial()
, the data is not displayed in the order of the data sent; it will be displayed in the database registration order.How should I do to display data in
ModelSelect2Multiple
in the sorted order in through model?ModelSelect2Multiple
works well when new data is registered.model.py
preview.py
forms.py
Registration order in datababase
Writer Mr.A(pk=1) Writer Mr.B(pk=2) Writer Mr.C(pk=3) Writer Mr.D(pk=4) The order of attendees in registered events(It will be successful at the initial registration)
Writer Mr.D(pk=4) Writer Mr.A(pk=1) Writer Mr.C(pk=3) The order displayed on the update view
Writer Mr.A(pk=1) Writer Mr.C(pk=3) Writer Mr.D(pk=4)
Environment: Django==1.11.6 django-autocomplete-light==3.2.10