python - Import one model inside urls.py from other app -
i trying import model app inside urls.py different app.
this code
main/settings.py
installed_apps = ( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'clients', 'rest_framework', )
main/urls.py
from django.conf.urls import url, include django.contrib import admin django.contrib.auth.models import user, group rest_framework import serializers rest_framework import viewsets rest_framework import routers myapp.clients.models import client class userserializer(serializers.hyperlinkedmodelserializer): class meta: model = user fields = ('url', 'username', 'email', 'groups') class groupserializer(serializers.hyperlinkedmodelserializer): class meta: model = group fields = ('url', 'name') class userviewset(viewsets.modelviewset): """ api endpoint allows users viewed or edited. """ queryset = user.objects.all() serializer_class = userserializer class groupviewset(viewsets.modelviewset): """ api endpoint allows groups viewed or edited. """ queryset = group.objects.all() serializer_class = groupserializer class clientviewset(viewsets.modelviewset): """ api endpoint allows users viewed or edited. """ queryset = client.objects.all() serializer_class = userserializer router = routers.defaultrouter() router.register(r'users', userviewset) router.register(r'groups', groupviewset) router.register(r'clients', clientviewset) urlpatterns = [ url(r'^', include(router.urls)), url(r'^admin/', include(admin.site.urls)), url(r'^api/', include('rest_framework.urls', namespace='rest_framework')) ]
clients/models.py
from django.db import models class client(models.model): phone_number = models.charfield(max_length=20) name = models.charfield(max_length=50) occupation = models.charfield(max_length=100) status = models.integerfield(default=1) number_of_requests = models.integerfield(default=0) karma = models.integerfield(default=5) member_from = models.datetimefield('member from') def __str__(self): # __unicode__ on python 2 return self.name
i getting error no module named 'myapp.clients'
main/urls.py in , line 25
that line: from myapp.clients.models import client
can advice how can fix this? new python, , struggling issue few hours. thanks!
clients
installed app, should able change line to:
from clients.models import client
Comments
Post a Comment