Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Django "repeatable" modelField in Admin

Coming from WordPress (CMB2 or ACF) I'm truly missing an easy way to make model fields repeatable in the Django Admin. Like lets say you are creating a team member page, and you don't know if a person has one or two email adresses or phone numbers. Or if you might need one for more url fields underneath, or perhaps you need to add more than one image?

The Django is nice, it easy er display things inline or stacked, etc. But it seems very fixed or locked out of box. And it doesn't help the usability if you have to populate the admin with duplicated fields that you might or might need.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The question is how would WP track and store the variable number of fields. The answer is one of two ways:

  • predefine the alternate optional entries, such as url1, url2, etc.
  • a separate Url model is created and the TeamMember class would point to many Url objects.

The same can be done in Django. It seems you would prefer not to predefine the fields, so using a ForeignKey from TeamMember to Url, as an example, you can set up the admin creation page for TeamMember to include a variable number of spare-if-needed Url fields.

See InlineModelAdmin objects for details.

Post back if you need more help. Good luck!!