Lets say I have a User who I want to allow to have many different
UserProfile objects (for multi-tenancy).

class User(models.Model):
    # other user fields ...

    profiles = models.ManyToManyField('UserProfile',
                        related_name='profiles', blank=True, null=True)

class UserProfile(models.Model):
    user = models.ForeignKey('User', related_name='profile_user')
    # other profile fields ...

This is fine but it will create a mapping table which for reasons I'll
explain later it creates a problematic layer of indirection between User
and UserProfile.  i.e. I'd like UserProfile be a through table for itself.

the reason this is problematic for me is that removing the mapping table
would make it much simpler in the admin.  for example lets say I have this
admin:

class UserProfileInline(admin.StackedInline):
    model = UserProfile
    extra = 0

class UserAdmin(admin.ModelAdmin):
    inlines = [UserProfileInline]

That provides the ideal user interface in the sense that I have a nice
inline edtitor for each of the fields in a UserProfile which hangs off the
User (in a 1-to-m).

However, it is actually wrong because it creates rows in the UserProfile
table directly and misses out the implicit mapping (aka "through") table.

I am aware that the correct way to do this is:

class UserProfileInline(admin.StackedInline):
    model = User.profiles.through
    extra = 0

However, this results in a non-ideal user-interface in the sense that I now
get a dropdown of all the existing mapping rows without any of the nice
inline create/edit for the UserProfile fields.

So to sum up:
1] is there a way in this case to just skip out the mapping table and just
have UserProfile be both the m2m table *and* the through table?

OR

2] is there a way to write the admin such that the "through" table expands
to an inline editor for the table the through targets rather than just the
list of mappings in the through table?

PS - in relation to #1 I came up with this whacky idea:

class User(models.Model):
    # other user fields ...

    profiles = models.ManyToManyField('UserProfile',
                                      related_name='profiles',
                                      through='UserProfile',
                                      through_fields=('user', 'self_id'),
                                      blank=True, null=True)


class UserProfile(models.Model):
    self_id = models.ForeignKey('self')
    user = models.ForeignKey('User', related_name='profile_user')
    # other profile fields ...

This seems like a potentially viable hack to accomplish what I'm
discussing.  i.e. User now "has many" UserProfile without any need for an
intermediate mapping table.  meaning I can get the ideal admin interface.

the rub is that I'd have to insert each UserProfile and then UPDATE the
self_id to have the same value as "id".

if through_fields allowed "id" I wouldn't have this problem.  but in my
Django version at least it doesn't allow that.  is there any way around
this?

Thanks for reading.

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

Reply via email to