对象没有属性“ get_url”

时间:2019-07-15 09:05:09

标签: django-rest-framework

大家好,我正在尝试使用序列化器生成模型api网址。收到一条错误消息,指出对象没有属性“ get_url”。 url不是我的模型中的字段。我不知道是否存在输入错误或不赞成使用get_url。是否存在一些错误,我正在观看使用django1.11且当前使用django2.0.6的教程,但是从现在开始我没有遇到太多问题。 这是我的代码:

serializers.py

from rest_framework import serializers
from MobApp.models import FieldData
from django.contrib.auth.models import User

class FieldDataSerializer(serializers.ModelSerializer):
    url = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = FieldData
        fields = [
            'url',
            'id',
            'OP_Timestamp',
            'OP_UserID',
            'OP_UnitID',
            'OP_GISObjTypeID',
            'OP_GISObjID',
            'OP_RecordTypeID',
            'OP_Comment',
            'OP_Sign',
            'OP_WO_TypeID',
            'OP_WO_GIS_ObjTypeID',
            'OP_WO_GIS_ObjID',
            'OP_WO_Site_x_ge',
            'OP_WO_Site_y_ge',
            'OP_Foto',
            'OP_Location',
            'OP_Meter_vol',
            'OP_Terr_chk',
            'OP_Terr_ok',
            'OP_Dump_chk',
            'OP_Dump_ok',
            'OP_Agr_chk',
            'OP_Agr_ok',
            'OP_Surr_chk',
            'OP_Surr_ok',
            'OP_Surr_Clean_done',
            'OP_Struct_chk',
            'OP_Struct_ok',
            'OP_Cl_chk',
            'OP_Cl_ok',
            'OP_Cl_done',
            'OP_Room_chk',
            'OP_Room_ok',
            'OP_Pump_chk',
            'OP_Pump_ok',
            'OP_Tank_chk',
            'OP_Tank_ok',
            'OP_Instal_chk',
            'OP_Instal_ok',
        ]
        read_only_fields = ['OP_Timestamp', 'OP_UserID', 'OP_UnitID']

        def get_url(self, obj):
            return obj.get_api_url()

models.py

from __future__ import unicode_literals
from django.db import models
from django.contrib.gis.db import models
from multiselectfield import MultiSelectField
from django.utils import timezone
from django.conf import settings
import datetime
from django.db.models.signals import pre_save, post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework.reverse import reverse as api_reverse


class UserProfile(models.Model):  
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    MMS_id = models.BigIntegerField(blank=True, null=True)

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.userprofile.save()


class FieldData(models.Model): 
    id = models.BigAutoField(primary_key=True)
    OP_Timestamp = models.DateTimeField(auto_now_add=True)
    OP_UserID = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    OP_UnitID = models.BigIntegerField(blank=True, null=True)
    OP_GISObjTypeID = models.BigIntegerField(blank=True, null=True)
    OP_GISObjID = models.BigIntegerField(blank=True, null=True)
    OP_RecordTypeID = models.BigIntegerField(blank=True, null=True)
    OP_Comment = models.BigIntegerField(blank=True, null=True)
    OP_Sign = models.BooleanField(default=False)
    OP_WO_TypeID = models.BigIntegerField(blank=True, null=True)
    OP_WO_GIS_ObjTypeID = models.BigIntegerField(blank=True, null=True)
    OP_WO_GIS_ObjID = models.BigIntegerField(blank=True, null=True)
    OP_WO_Site_x_ge = models.FloatField(blank=True, null=True)
    OP_WO_Site_y_ge = models.FloatField(blank=True, null=True)
    OP_Foto = models.CharField(max_length=254, blank=True, null=True)
    OP_Location = models.CharField(max_length=254, blank=True, null=True)
    OP_Meter_vol = models.FloatField(max_length=254, blank=True, null=True)
    OP_Terr_chk = models.BooleanField(default=False)
    OP_Terr_ok = models.BooleanField(default=False)
    OP_Dump_chk = models.BooleanField(default=False)
    OP_Dump_ok = models.BooleanField(default=False)
    OP_Agr_chk = models.BooleanField(default=False)
    OP_Agr_ok = models.BooleanField(default=False)
    OP_Surr_chk = models.BooleanField(default=False)
    OP_Surr_ok = models.BooleanField(default=False)
    OP_Surr_Clean_done = models.BooleanField(default=False)
    OP_Struct_chk = models.BooleanField(default=False)
    OP_Struct_ok = models.BooleanField(default=False)
    OP_Cl_chk = models.BooleanField(default=False)
    OP_Cl_ok = models.BooleanField(default=False)
    OP_Cl_done = models.BooleanField(default=False)
    OP_Room_chk = models.BooleanField(default=False)
    OP_Room_ok = models.BooleanField(default=False)
    OP_Pump_chk = models.BooleanField(default=False)
    OP_Pump_ok = models.BooleanField(default=False)
    OP_Tank_chk = models.BooleanField(default=False)
    OP_Tank_ok = models.BooleanField(default=False)
    OP_Instal_chk = models.BooleanField(default=False)
    OP_Instal_ok = models.BooleanField(default=False)

    def __unicode__(self):
        return self.id

    @property
    def owner(self):
        return self.OP_UserID

    def get_api_url(self):
        return api_reverse("api-urls:post-rud", kwargs={'pk': self.pk})

error.log

AttributeError at /api/field_data/1/

'FieldDataSerializer' object has no attribute 'get_url'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/api/field_data/1/
Django Version:     2.0.6
Exception Type:     AttributeError
Exception Value:    

'FieldDataSerializer' object has no attribute 'get_url'

Exception Location:     C:\Users\Genti\Anaconda3\envs\MMS\lib\site-packages\rest_framework\fields.py in to_representation, line 1854
Python Executable:  C:\Users\Genti\Anaconda3\envs\MMS\python.exe
Python Version:     3.6.5
Python Path:    

['C:\\Users\\Genti\\Anaconda3\\envs\\MMS\\geodjango',
 'C:\\Users\\Genti\\Anaconda3\\envs\\MMS\\python36.zip',
 'C:\\Users\\Genti\\Anaconda3\\envs\\MMS\\DLLs',
 'C:\\Users\\Genti\\Anaconda3\\envs\\MMS\\lib',
 'C:\\Users\\Genti\\Anaconda3\\envs\\MMS',
 'C:\\Users\\Genti\\Anaconda3\\envs\\MMS\\lib\\site-packages']

Server time:    Mon, 15 Jul 2019 09:29:19 +0000

1 个答案:

答案 0 :(得分:0)

您的get_url()方法现在位于类Meta中。在FieldDataSerializer()类中创建

您的代码:

class FieldDataSerializer():
    ****
    class Meta():
        *****
        def get_url()

正确的代码:

class FieldDataSerializer():
    ****
    class Meta():
        *****
    def get_url()