python - Django + Django-Rest framework cannot add M2M relation between objects -


i developing rest api drf , have issue when try add m2m relation between existing objects in db error:

raise appregistrynotready("models aren't loaded yet.") django.core.exceptions.appregistrynotready: models aren't loaded yet. 

everything works fine when try same thing console. problem occurs when execute method:

@staticmethod def update_sockets():     c1 = apicountry.objects.all()[3]     s1 = sockettype.objects.all()[2]     c1.sockettype.add(s1)     c1.save() 

models are:

class sockettype(models.model):     name = models.charfield(max_length=1, blank=true)     country = models.manytomanyfield(apicountry, blank=true, related_name='sockettype')  class apicountry(models.model):     countrycode = models.charfield(max_length=5, unique=true, blank=true)     name = models.charfield(max_length=50, blank=true) 

i had similar issue. try add these lines @ beginning of program

import django

django.setup()

reference: https://docs.djangoproject.com/en/1.8/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -