简体   繁体   中英

How to use custom models in Django?

Followed the Django Quickstart tutorial here and it all worked wo/ problems.

Now when I added a custom APIView I started having trouble. Namely the custom models I created.

Here is my views.py

class ValuationDetails(APIView):

   def get(self, request, format=None):
       serializer = ValuationRequestSerializer(data=request.data)
       if serializer.is_valid():
           return Response('Valuation report', status=status.HTTP_201_CREATED)

Here is my ValuationRequestSerializer:

class ValuationRequestSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = ValuationRequest
        fields = ('street', 'number', 'latitude', 'longitude')

Here is my ValuationRequest:

from django.db import models

class ValuationRequest(models.Model):
    def __init__(self, street, number, latitude, longitude):
        self.street = street
        self.number = number
        self.latitude = latitude
        self.longitude = longitude

The first exception was:

RuntimeError: Model class valuationservice.valproperty.model.val_models.ValuationRequest doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

Then I added in the setting.py INSTALLED_APPS a line

'valuationservice.valproperty.model.val_models'

The second exception after that was:

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

I guess there are two questions:

  1. How can I use custom models in Django?
  2. What is missing that the Django doesn't like by throwing the first exception?

Python version 2.7.12

Thanks!

You should not define an __init__ method in your model class.

(If you did, you should always be sure to call the superclass method using super() ; but you shouldn't do it at all in this case.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM