Search code examples
pythondjangomodels

How do i import list of variables as choices for a charfield from outside the app?


I'm trying to make a select box in the admin that shows a list of objects in the database, outside the current app. Here is my model

from typefaces.models import Typeface

class Word(models.Model):
    text = models.CharField(max_length=200)
    family_select = models.CharField(max_length=100, choices=Typeface.objects.all)

Unfortunately, Django tells me 'choices' must be an iterable (e.g., a list or tuple). But my attemps to make it iterable with iter() have yielded no success.


Solution

  • This is a completely wrong way to do it. Relationships between models should be specified using OneToOneFields, ForeignKeys (one-to-many field) and ManyToManyFields. You should just change the CharField to this:

    family = models.ForeginKey(Typeface, related_name='words')
    

    If you have a specific reason for not using the generally acceptable way, please elaborate on that further to get an answer for that.