I have a model with a ManyToMany field, but when I make a query, this field returns None even when it has values in the database (I have checked it with Django Admin util).
This is my model:
class Profile(models.Model):
name = models.CharField(max_length=50, unique=True, verbose_name=_('Name'))
cities = models.ManyToManyField(City, related_name='profiles', verbose_name=_('City'))
class Meta:
verbose_name = _('Profile')
verbose_name_plural = _('Profiles')
def __str__(self):
return "[{}]{}".format(self.name, self.cities)
This is my query:
profile_city = models.Profile.objects.get(id=profile_id)
print(profile_city.id, ' ', profile_city.name, ' ', profile_city.cities)
And this is the result in console:
1 Quintana Roo GeneralApp.City.None
When I try to iterate over ´Profile.cities´, I get this error: ´TypeError: 'ManyRelatedManager' object is not iterable´.
I am new to Django and I thought ManyToMany fields returned an array, but It is not, apparently.
Thanks for your help.