Reputation: 789
I'm having problems to return my object after I add some items into my "countries" ManytomanyField. I can see my data being saved
class CompanyProfileManager(models.Manager):
@transaction.atomic
def register_company(self, name, description, tag_name, email, is_private, uses_credits, pays_subscription, pool,
facebook, twitter, web, country, videocall_doctor_to_patient, micro_type, can_move, fixed_percentage, fixed_price, fixed_tariff, instagram, subdomain, banner_description, banner_title, countries, **kwargs):
tag = Tag(name=tag_name, description=tag_name, is_active=True)
tag.save()
company = self.create_instance(name, description, email, is_private, uses_credits, pays_subscription, pool,
facebook, twitter, web, country, videocall_doctor_to_patient, micro_type, can_move, fixed_percentage, fixed_price, fixed_tariff, instagram, subdomain, banner_description, banner_title)
company.tag = tag
company.save()
for item in countries:
company.countries.add(item)
return company #Using Debug Mode, My project breaks right here
I'm really lost, the only thing I've seen so far related to my problem is using .all() to get as a queryset
Upvotes: 5
Views: 11185
Reputation: 1984
ManyToMany attributes (in your case countries
) are Manager
, more exactly ManyRelatedManager
and not QuerySet
. A bit like objects
in QuerySet.objects
so you need to use .all()
if you want to iterate through all your countries:
for item in countries.all():
company.countries.add(item)
You can use other fancy methods as well like .filters()
, .select_related()
, etc...
EDIT
Since the variable countries
is not a queryset but a list, I suspect that the error is not generated byt this chunk of code, probably just after when you try to iterate through company.countries
instead of company.countries.all()
Upvotes: 13