Foreign key model in Django Admin
<p>I have two user roles in Django:</p>

<ul>
<li>Commercials</li>
<li>Sellers</li>
</ul>

<p>I have created two models, Seller Model has a ForeignKey field to Commercials (every seller has a commercial related to). When I register the models in admin I can create Commercials and related sellers using <code>StackedInline</code>, <code>TabularInline</code> etc.</p>

<p>The problem I have is I need to associate users to this models in order to authenticate, login, etc. In admin I need to create a user (in an inline way, not dropdown box)</p>

<p>This is my code:</p>

<p>In <strong>models.py</strong>:</p>

<pre><code>class Commercial(models.Model):
    name = models.CharField(max_length=255, null=True)
    user = models.OneToOneField(User, null=True)


class Seller(models.Model):
    name = models.CharField(max_length=255, null=True)
    commercial = models.ForeignKey('Commercial')
    user = models.OneToOneField(User, null=True)
</code></pre>

<p>In <strong>admin.py</strong>:</p>

<pre><code>class SellerAdmin(admin.StackedInline):
    model = Seller
    extra = 1

class CommercialAdmin(admin.ModelAdmin):
   inlines = [SellerAdmin]

admin.site.register(Commercial, CommercialAdmin)
</code></pre>

<p>I need to edit, create, users etc. related to this models inline not in a modal window, Is there any way?</p>

<p><a href="http://i.stack.imgur.com/w54tZ.png" rel="nofollow"><img src="http://i.stack.imgur.com/w54tZ.png" alt="enter image description here"></a></p>

<p>There is no way to make a reverse relation (so to say) in the form of Inlines. The Django admin panel doesn't have that ability by default.</p>

<p>What you could do is unregister the default UserAdmin, create a new Admin panel by inheriting the original one, and add this Seller as an Inline. There is still the issue that Django doesn't support multiple inlines, hence, you will not be able to use the Commercial model in the same admin page.</p>

<p>To fix that, you could refer to <a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form" rel="nofollow">this information from the doc</a> which shows how to overwrite the automatically created ModelForm for a particular ModelAdmin. </p>

<p>This will however not be that helpful as they are a lot of work. I would rather suggest implementing a workaround in how your application is used, rather than complicating it this much. Depends on the needs of the project, whether you need to go through this much trouble</p>

python django django-models django-admin