B
B
bubo2015-12-21 13:43:14
Django
bubo, 2015-12-21 13:43:14

How to add an unlimited number of branches to the organization model in the django admin?

Hello. At me such problem
Is the organization and there are its branches. Each organization can have several branches, and therefore in the admin panel it was necessary to make sure that the fields of branches are added dynamically.
To do this, I created 2 models (Organization, Branches) and linked them in the admin panel via admin.StackedInline
models.py

class Branches(models.Model):
    phone = models.CharField('Телефон', max_length=50)
    address = models.CharField('Адрес', max_length=50)
    branch = models.ForeignKey('Organization', null=True, blank=True)

class Organization(models.Model):
    title = models.CharField('Заголовок', max_length=50)
    phone = models.CharField('Телефон', max_length=50)
    address = models.CharField('Адрес', max_length=50)

admin.py
class BranchesInline(admin.StackedInline):
    model = Branches
    extra = 1

class OrganizationAdmin(admin.ModelAdmin):
    inlines = [BranchesInline]

Then it was necessary to make sure that organizations and branches could be uploaded through an excel file. For this, I installed Django-import-export , but it doesn't populate the branches, since the branches belong to a different model.
Are there any other ways to add branches to organizations?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
belanchuk, 2015-12-21
@belanchuk

Change this:
on this:

branch = models.ManyToManyField('Organization', null=True, blank=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question