N
N
Nikita Roschenko2015-08-31 19:45:58
Django
Nikita Roschenko, 2015-08-31 19:45:58

Why is ManyToManyField not displayed in django admin if I specify through?

Hello, I have 3 models:

class Account(models.Model):
    login = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    first_name = models.CharField(max_length=255, null=True, blank=True)
    last_name = models.CharField(max_length=255, null=True, blank=True)
    profile_photos = models.ManyToManyField(Photo, related_name='profile_photo', through='ProfilePhoto', blank=True)


class Photo(models.Model):
    photo_name = models.CharField(max_length=255)
    photo_url = models.CharField(max_length=255)


class ProfilePhoto(models.Model):
    account = models.ForeignKey(Account)
    photo = models.ForeignKey(Photo)

I want to edit the profile_photos links in the django admin when editing the Account model. But in the admin panel I simply do not have this field, it is not displayed. But if I remove the through parameter, then the field appears. Interestingly, if I try to edit the Account object from the console, then the profile_photos field is there and available. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey K, 2015-08-31
@mututunus

Why not do so?

class Account(models.Model):
    login = models.CharField(max_length=255)
    password = models.CharField(max_length=255)
    first_name = models.CharField(max_length=255, null=True, blank=True)
    last_name = models.CharField(max_length=255, null=True, blank=True)

class ProfilePhoto(models.Model):
    account = models.ForeignKey(Account)
    photo_name = models.CharField(max_length=255)
    photo_url = models.CharField(max_length=255)

D
Dmitry Seliverstov, 2017-01-23
@DmitriySE

This is due to the peculiarity of the work of admin widgets in Django. One way to override the inline class of your models is like this:

class ProfilePhotoInline(admin.TabularInline):
    model = ProfilePhoto
    extra = 1


class AccountAdmin(admin.ModelAdmin):
    inlines = (ProfilePhotoInline,)


class ProfilePhotoAdmin(admin.ModelAdmin):
    inlines = (ProfilePhotoInline,)

Further, in the same place, we simply register our models:
admin.site.register(Account, AccountAdmin)
admin.site.register(ProfilePhoto, ProfilePhotoAdmin)

Read more in the documentation

S
Stanislav Vladimirovich, 2019-04-26
@mrFoggg

the same problem. I want to use the filter_horizontal output in the admin
without through does not create the intermediate table promised in the documentation, and the output does not work with through. The bottom line is that there are two models: products (in them I will display 1-2 related categories through ForeignKey - rows of the intermediate table) and categories in which related products are displayed using filter_horizontal since there are a lot of products in the category. Products and categories are interconnected by M2M (respectively, with an intermediate table through ForeignKey in it).
try here:
https://pypi.org/project/django-sortedm2m-filter-h...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question