Hi Gabriel,

Yes, the hope is to make the situation better and more standard. Here's
some of the status:
https://code.djangoproject.com/ticket/24317 - real reverse fields.
https://code.djangoproject.com/ticket/897 - real reverse many to many fields
https://code.djangoproject.com/ticket/12203 - many to many with through
tables

Also, filtering fields on the .multiple attribute might help you out. Not
sure though. If it isn't, maybe open a pull request with a proposal for an
attribute that would work for you?

Quoting the advice from the first ticket:
- Find places where field.remote_field responds to different API than Field.
- Fix these one at a time while trying to have backwards compat, even if
the API isn't public.
- In addition, simplifications to the APIs are welcome, as is a high level
documentation of how related fields actually work.
- We need to try to keep backwards compat as many projects are forced to
use the private APIs.
- But most of all, do small incremental changes.

As far as your specific use-case goes, "a widget that is a table with
inputs for cells". The closest thing Django has is an "inline formset"
(though not actually a form field) which is designed exactly for reverse
relationships. You would say:

ThroughFormSet = inlineformset_factory(MainModel, ThroughModel)

https://docs.djangoproject.com/en/1.11/topics/forms/modelforms/#inline-formsets
(Or in the admin, you just use an inline which uses an inlineformset under
the hood.)

Collin


On Thu, Mar 30, 2017 at 3:30 PM, Gabriel Canto <ppgab.m...@gmail.com> wrote:

> When you are creating a system from scratch, coming with a ideal and
> organized model is simple enough, but when you are trying to replicate a
> real world system/issue into your models, things get complicated.
>
> By complicated I mean, almost every relationship is a M2M with
> intermediary fields, that's the issue I'm trying to solve using Django,
> real world documents, contracts, people.
>
> The main concern is making the CRUD web app easy for the user eyes.
>
> Django really made things difficult, specially because in our context the
> relationship direction doesn't matter, we want both forward and reverse
> fields when editing an object
>
>
> *First suggestion: give reverse relationships a common attribute for an
> easier way of getting them*
>
> This is the code I had to make to get only reverse relationships of a
> model, it is very ugly and not my proudest work, but it works(so far)
>
>  #I'm sure there's a better way of doing this
>  #This is the documented way of getting reverse fields, but it also gets
> ManyToOne rel with the through table of forward m2m rels, so we have to
> filter them
>  reverse_fields = [f for f in opts.get_fields() if f.auto_created and not
> f.concrete]
>  reverse_m2m = [f for f in reverse_fields if f.__class__.__name__ ==
> 'ManyToManyRel' and f.on_delete == None and not getattr(model,f.field.name
> ,False)]
>  #Also filtering the ManyToOne rel with the through table of the reverse
> m2m rels, because we need to differentiate them from reverse ManyToOne with
> other models(not through)
>  reverse_m2m_through = [f for f in reverse_fields if f.__class__.__name__
> == 'ManyToOneRel'  and f.related_model in [f.through for f in reverse_m2m
> if getattr(f,'through',False)]]
>  forward_m2m = [f.remote_field for f in opts.get_fields() if not 
> f.auto_created
> and f.many_to_many]
>  #Filtering the ManyToOne rels with the through table of forwards m2m
> rels, same logic as above
>  forward_m2m_through = [f for f in reverse_fields if f.__class__.__name__
> == 'ManyToOneRel'  and f.related_model in [f.through for f in forward_m2m
> if getattr(f,'through',False)]]
>  reverse_fields_final = itertools.chain(reverse_m2m, [f for f in
> reverse_fields if f not in itertools.chain(reverse_m2m,
> reverse_m2m_through, forward_m2m, forward_m2m_through)])
>
> *Second suggestion: following the first suggestion, give support to edit
> reverse rels in forms, and m2m with through tables*
>
> An optional argument to ModelForm to also catch reverse relationships,
> using a specific form field for it, example : ReverseModelChoiceField,
> etc..., this can be done with just a simple changes to their forward
> counterpart classes
>
> And, the most complex suggestion probably, is to add a form field to deal
> with M2M with through tables that contain intermediary fields, this can be
> done with a widget that is a table with inputs for cells, plus also work
> with reverse as suggested above, so I'm suggesting the creation of the 5
> following model form fields :
>
>
>    1. ReverseModelChoiceField
>    2. ReverseMultipleModelChoiceField
>    3. ReverseManyToManyWithIntermediaryField (I'm sure there's a better
>    name)
>    4. ManyToManyWithIntermediaryField
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To post to this group, send email to django-developers@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-developers/4b82d7d5-3a8d-4130-97b6-
> dff5423f9372%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/4b82d7d5-3a8d-4130-97b6-dff5423f9372%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAFO84S6i0EJxFhVqXRa1KcUqRDQ05CLTxWBVA7%3DkYAV3gaULLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to