Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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)
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,)
admin.site.register(Account, AccountAdmin)
admin.site.register(ProfilePhoto, ProfilePhotoAdmin)
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 questionAsk a Question
731 491 924 answers to any question