Gent Bytyqi
Gent Bytyqi

Reputation: 419

object has no attribute 'get_url'

Hello guys I'm trying to generate the model api url using a serializer. Getting an error saying object has no attribute 'get_url'. url is not a field in my model. I don't know if there is a typing error or the get_url is deprecated. Is there some error, I'm watching a tutorial which uses django1.11 and currently using django2.0.6 but since now I didn't get much problem. This is my code:

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

Upvotes: 0

Views: 1027

Answers (1)

Ararat Stepanjan
Ararat Stepanjan

Reputation: 75

your get_url() method is now in class Meta. Make it in FieldDataSerializer() class

your Code:

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

correct Code:

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

Upvotes: 2

Related Questions