Django admin - Inline with choices from database
<p>I need some basic help with the django admin site. What I basically want to do is to be able to populate an inline with choices from the database. For example consider the following models:</p>

<pre><code>class Item(models.Model):
    description = models.CharField(max_length=100)

class Category(models.Model):
    name = models.CharField(max_length=100)
    item = models.ForeignKey(Item, on_delete=models.CASCADE, null=True, blank=True)
</code></pre>

<p>And in <strong>admin.py</strong> I have the following setup:</p>

<pre><code>class CategoryAdminForm(forms.ModelForm):
  name = forms.ChoiceField(choices = category_service.get_all_categories())

class CategoryInline(admin.TabularInline):
  model = Category
  form = CategoryAdminForm

class ItemAdmin(admin.ModelAdmin):
  inlines = [CategoryInline]

admin.site.register(Item, ItemAdmin)
admin.site.register(Category)
</code></pre>

<p>What I want to be able to do is to insert categories into db, and when I want to insert an item, the categories inline to be populated with categories from the db. </p>

<p>With the current setup it is not working. It says that category is not an iterable object. What am I missing here?</p>

<p>You should replace your <code>ChoiceField</code> with a <a href="https://docs.djangoproject.com/en/1.10/ref/forms/fields/#modelchoicefield" rel="nofollow"><code>ModelChoiceField</code></a>. They allow you to specify a queryset to populate the choices.</p>

<pre><code>category = forms.ModelChoiceField(queryset=Category.objects.all(), empty_label="(Nothing)")
</code></pre>

python django django-admin