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.
For more options, visit https://groups.google.com/d/optout.

Reply via email to