L
L
lukepker2019-07-30 02:01:07
Django
lukepker, 2019-07-30 02:01:07

How to add inline link to user in django admin?

Hello!
I registered the m2m connection with the user in the models:

from django.contrib.auth.models import User

class Item(models.Model):

    users = models.ManyToManyField(User, through='Item_in_cart', verbose_name='Заказавшие', related_name='items')

class Item_in_cart(models.Model):

    item = models.ForeignKey(Item, verbose_name='Товар', on_delete=models.CASCADE)
    user = models.ForeignKey(User, verbose_name='Пользователь', on_delete=models.CASCADE)
    amount = models.IntegerField(verbose_name='Количество')

Now I need Item_in_cart to be displayed in the user in the admin panel (it is in the user, it is not suitable for the item. For this I write
from django.contrib.auth.models import User

class ItemInline(admin.TabularInline):
    model = Item_in_cart
    extra = 1

class UserAdmin(admin.ModelAdmin):
    inlines = (ItemInline, )

admin.site.register(User, UserAdmin)

gives an error django.contrib.admin.sites.AlreadyRegistered: The model User is already registered It
seems logical that the user is already registered in the admin panel, but how to enter the necessary one?
Tell me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@bickford, 2019-07-30
@lukepker

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question