运行迁移后,不会在DB中创建Django模型

时间:2016-10-26 21:59:53

标签: python django-models

我想从API获取数据并将其存储在DB中。但是api url不应该被硬编码。所以我创建了api.py,其中调用url请求如下:

class API(object):
def __init__(self, key, base_url=None):
    self._key = key
    self._base_url = base_url
def get_predictor(self):
    payload = {'key': self._key, 'results':'100'}
    response = requests.get(
    self._get_url(),
    data=payload,
    timeout=(3.1, 10))
    self._get_url_predictor()
def _get_url_predictor(self):
    return '/'.join([self._base_url, 'api', 'meter', 'feed.json'])

然后我创建了一个文件预测器,我将参数传递给url并从API获取详细信息,如下所示:

import urllib,json
from django.conf import settings
from AppSerializer.models import EnergyOTAPI, PowerPrediction
from AppSerializer.meter.api import API
def update_api_predictor():
for meter in EnergyOTAPI.objects.all():
            get_api_predector_data(meter)
def get_api_predector_data(meter):
api = API (
    key=meter.key,
    base_url=settings.SOURCE_ENDPOINTS['url_api'][0]
)
endpoint = api.get_predictor()
serialized_data = urllib.request.urlopen(endpoint).read()
output = json.loads(serialized_data)
def _create_sp_power_prediction_from_api():
    created_at = output['channels'][0]['feeds'][0]['created_at']
    entry_id = output['channels'][0]['feeds'][0]['entry_id']
    value = output['channels'][0]['feeds'][0]['value']
    PowerPrediction.objects.create(created=created_at,
                                        entry=entry_id,
                                        value=value)

然后,对于那些字段,我创建了模型,序列化程序,视图和URL,如下所示。

模型:

from django.db import models
class EnergyOTAPI:
key = models.CharField(max_length=255)
class PowerPrediction:
created = models.DateField()
entry = models.IntegerField('max_length=500')
value = models.IntegerField('max_length=500')

串行:

from rest_framework import serializers

from AppSerializer.models import PowerPrediction

class PowerPredictonSerializer(serializers.HyperlinkedModelSerializer):
    source = serializers.ReadOnlyField(source='source.pk')
    class Meta:
        model = PowerPrediction
        fields = ('created',
                  'entry',
                  'value')

查看:

from rest_framework.decorators import api_view
from rest_framework import generics, permissions, viewsets
from rest_framework.reverse import reverse
from rest_framework.response import Response
from AppSerializer.models import PowerPrediction
from AppSerializer.serializer import PowerPredictonSerializer

@api_view(['GET'])
def api_root(request, format=None):
return Response({
    'locations': reverse('solar:location-list', request=request,      format=format),
 'sources': reverse('source-list', request=request, format=format)
})

class PowerPredictionList(generics.ListAPIView):
queryset = PowerPrediction.objects.all()
serializer_class = PowerPredictonSerializer
def perform_create(self, serializer):
    serializer.save(location=self.request.location)

class PowerPredictionDetail(generics.RetrieveDestroyAPIView):
queryset = PowerPrediction.objects.all()
serializer_class = PowerPredictonSerializer

1 个答案:

答案 0 :(得分:1)

您的模型不会扩展Django django.db.models.Model类。

您从模型文件顶部的models导入django.db,因此您只需更改models.py文件,如下所示:

class EnergyOTAPI(models.Model):
    key = models.CharField(max_length=255)

class PowerPrediction(models.Model):
    created = models.DateField()
    entry = models.IntegerField('max_length=500')
    value = models.IntegerField('max_length=500')