Sheraram_Prajapat
Sheraram_Prajapat

Reputation: 597

Limiting ForeignKey fields in admin when editing section/site Django

how to limit the number of foreign key fields in Django next time editing? example : I've a model Project and Images, Images has many to one relation to Project. Project includes title, slug, description, etc.

models.py

class Images(models.Model):
    image = ImageField(upload_to='project/')
    multi_image = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='other_images')

admin.py

class ImagesInline(AdminImageMixin, admin.TabularInline):
    model = Images
    extra = 3

I can register this class using inline.

before saving before saving

after saving and visiting again for editing

editing

I want to remove those extra empty image fields when I go for editing, I mean I don't want to show them anymore. Is it possible?

Upvotes: 0

Views: 31

Answers (1)

Nico Griffioen
Nico Griffioen

Reputation: 5405

You can set a value max_num on ImagesInline, which will limit the maximum number of inlines displayed. If believe if this number is set and you already have some filled in relationships, it will fill it to the max number (So in the case of your screenshot, if you set it to 3, it will show 1 filled in relationship and 2 blanks.)

Upvotes: 0

Related Questions