Django Rest Framework - Could not resolve URL for hyperlinked relationship using view name "field-detail" - serialization

I know that this inconvenient is very presented, may be that I need learn more about of serializer relationships
I have the following model:
class Field(models.Model):
FIELD_TYPE_NATURE = 'Grama natural'
FIELD_TYPE_ARTIFICIAL = 'Grama sintetica'
FIELD_TYPE_CHOICES = (
(FIELD_TYPE_NATURE, u'Grama natural'),
(FIELD_TYPE_ARTIFICIAL, u'Grama sintetica'),
)
MODALITY_11 = 'Fútbol 11'
MODALITY_8 = 'Fútbol 8'
MODALITY_CHOICES = (
(MODALITY_11, u'Fútbol 11'),
(MODALITY_8, u'Fútbol 8'),
)
name = models.CharField(
max_length=150,
unique=True,
db_index=True,
primary_key=True,
)
field_type = models.CharField(
choices=FIELD_TYPE_CHOICES,
default=False,
blank=False,
max_length=20,
verbose_name=('Tipo de material/grama de la cancha')
)
modality = models.CharField(
max_length=40,
blank=False,
verbose_name='Modalidad'
)
photo = models.ImageField(upload_to='fields', blank=True, null=True)
location = models.CharField(max_length=150, blank=False)
def __str__(self):
return '%s %s %s' % (self.name, self.field_type, self.location)
My serializer is the following:
class FieldSerializer(serializers.HyperlinkedModelSerializer):
#url = serializers.HyperlinkedIdentityField(view_name='field-detail',)
class Meta:
model = Field
fields = ('url', 'name','field_type','modality','photo','location')
My viewset is:
class FieldViewSet(viewsets.ModelViewSet):
queryset = Field.objects.all()
serializer_class = FieldSerializer
This is my router:
router = routers.DefaultRouter()
router.register(r'fields', FieldViewSet)
And my url:
...
url(r'^api/', include(router.urls)),
...
When I go to the http://localhost:8000/api/fields/ url I get the following message:
File "/home/bgarcial/.virtualenvs/fuupbol2/lib/python3.5/site-packages/rest_framework/relations.py", line 386, in to_representation
raise ImproperlyConfigured(msg % self.view_name)
django.core.exceptions.ImproperlyConfigured: Could not resolve URL for **hyperlinked relationship using view name "field-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.**
[11/Nov/2016 16:39:53] "GET /api/fields/ HTTP/1.1" 500 187477
When I use HyperlinkedIdentityField in my FieldSerializer class:
class FieldSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='field-detail',)
class Meta:
model = Field
fields = ('url', 'name','field_type','modality','photo','location')
I follow getting the same error.
Althought when I go to the url http://localhost:8000/api/fields/ I want get is a list of my objects, then is possible that I should put:
url = serializers.HyperlinkedIdentityField(view_name='field-list',)
?
I use HyperlinkedIdentityField according to:
This field can be applied as an identity relationship, such as the
'url' field on a HyperlinkedModelSerializer. It can also be used for
an attribute on the object.
I put the field-list in my view_name attribute and I get the error related
Could not resolve URL for hyperlinked relationship using view name "field-list". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
I don't understand the situations when I should use view_name attribute in relation to if I wnt get a list objects, a object detail and so ... although here explain something about it.
When I should use HyperlinkedModelSerializer and ModelSerializer

Related

How to get value on one field based on two different fields

So Im creating a hotel management module . I have the option to filter rooms based on bed_type and tag. Tag contains different facilities like AC, TV etc. So an user will come and select a bed_type and the facilities he wants and in the third field it should show the rooms that have the given configuration if not available an error messages should come. SO i created a onchange function to do this , but i dint know how to include the tags in it. Im doing it on odoo v14. m
This is the model for the room
from odoo import api, fields, models, _
class HotelRoom(models.Model):
_name = 'hotel.room'
_description = 'hotel room'
_rec_name = 'room_number'
room_number = fields.Char('Room Number', required=True)
room_rent = fields.Monetary('Room Rent')
tag = fields.Many2many('hotel.tags', string='Facilities')
dormitory_count = fields.Integer('Dormitory count')
bed_type = fields.Selection([
('single', 'Single'),
('double', 'Double'),
('dormitory', 'Dormitory')
], required=True, default='other')
This is the model for the reception
class HotelAccommodation(models.Model):
_name = 'accommodation.room'
_description = 'Reception'
bed_type = fields.Selection([
('single', 'Single'),
('double', 'Double'),
('dormitory', 'Dormitory')
], required=True, string= 'Bed type')
state = fields.Selection([
('draft','Draft'),
('check-in','Check-In'),
('check-out', "Check-Out"),
('cancel','Cancel'),
], required=True, default='draft', tracking=True)
tag = fields.Many2many('hotel.tags', string='Facilities')
room_id = fields.Many2one('hotel.room', string='Room')
Using this I can filter the rooms based on the bed_type but I need to have the tags as well.I tried giving it inside the domain but its not working .
#api.onchange('bed_type')
def filter_room(self):
for rec in self:
return {'domain': {'room_id': [('bed_type', '=', rec.bed_type)]}}
You need also to add the tag field to the domain and to the onchange decorator, so the method will be called when the tag field is modified.
The method is invoked on a pseudo-record that contains the values present in the form, you do not need to loop over self.
Try the following example:
#api.onchange('bed_type', 'tag')
def filter_room(self):
return {'domain': {'room_id': [('bed_type', '=', self.bed_type), ('tag', 'in', self.tag.ids)]}}
You can use _compute fields with the store option = True
take a look https://www.odoo.com/documentation/14.0/reference/orm.html#computed-fields

After migrating the models in django, I am unable to see the data in admin panal

I am trying to solve this problem whole day. My code is shown below. Actually, I created my tables in Pgadmin4. I created the table "atmpokhara" having 10 columns with id field as varchar(20) datatype. After that, I drop the id filed from my atmpokhara table. I render this table to django model using
python manage.py inspectdb > models.py
All the row are already in tables. Everything is going good, but when I try to open my table inside my admin panel, I get following error:
operator does not exist: character varying = bigint
LINE 1: ...number" FROM "atmpokhara" WHERE "atmpokhara"."id" = 57173664...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Request Method: GET
Request URL: http://localhost:8000/admin/webmap/atmpokhara/5717366409/change/
Django Version: 2.1.7
Exception Type: ProgrammingError
Exception Value:
operator does not exist: character varying = bigint
LINE 1: ...number" FROM "atmpokhara" WHERE "atmpokhara"."id" = 57173664...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
Exception Location: D:\coding time\Django+ GeoDjango\Django Enviroment\venv\lib\site-packages\django\db\backends\utils.py in _execute, line 85
Python Executable: D:\coding time\Django+ GeoDjango\Django Enviroment\venv\Scripts\python.exe
Python Version: 3.7.2
Python Path:
['C:\\Users\\Tekson\\Desktop\\Web-GIS-Django\\Project',
'C:\\Program Files\\Hexagon\\ERDAS IMAGINE '
'2015\\usr\\lib\\Win32Release\\python',
'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\Tekson\\AppData\\Local\\Programs\\Python\\Python37',
'D:\\coding time\\Django+ GeoDjango\\Django Enviroment\\venv',
'D:\\coding time\\Django+ GeoDjango\\Django '
'Enviroment\\venv\\lib\\site-packages']
Server time: Tue, 16 Jul 2019 12:23:38 +0000
Please help me to findout the solution.
models.py file
from django.contrib.gis.db import models as gis_models
# Create your models here.
class point(models.Model):
title = models.CharField(max_length=30)
location = gis_models.PointField(srid=4326)
def __str__(self):
return self.title
class Atmpokhara(models.Model):
bankname = models.CharField(max_length=150, blank=True, null=True)
nepalibankname = models.CharField(max_length=150, blank=True, null=True)
address = models.CharField(max_length=150, blank=True, null=True)
email = models.CharField(max_length=50, blank=True, null=True)
banktype = models.CharField(max_length=30, blank=True, null=True)
source = models.CharField(max_length=50, blank=True, null=True)
operator = models.CharField(max_length=150, blank=True, null=True)
geom = gis_models.PointField(blank=True, null=True)
phonenumber = models.CharField(max_length=50, blank=True, null=True)
class Meta:
managed = False
db_table = 'atmpokhara'
my admin.py file:
from .models import point,Atmpokhara
from leaflet.admin import LeafletGeoAdmin
# Register your models here.
class pointsAdmin(LeafletGeoAdmin):
list_display = ('title', 'location')
class AtmpokharaAdmin(LeafletGeoAdmin):
list_display = ('bankname', 'address', 'banktype')
admin.site.register(point,pointsAdmin)
admin.site.register(Atmpokhara, AtmpokharaAdmin)
my views.py:
from django.core.serializers import serialize
from django.http import HttpResponse
from .models import Atmpokhara
# Create your views here.
def index(request):
return render(request, 'pages/index.html')
def atm(request):
atmData = serialize('geojson', Atmpokhara.objects.all())
return HttpResponse(atmData, content_type='json')
my url.py file:
from . import views
urlpatterns = [
path('', views.index, name='webmap'),
path('data/atm', views.atm, name= 'atm'),
]

Marshmallow - Sort field values by declared order

I've read the docs and searched this site but cannot seem to find a solution to sorting field values by the order in which they are declared. The docs state that adding ordered = True to the class Meta will solve this problem -
class MySchema(Schema):
class Meta:
ordered = True
However, I am not using class Meta in my schema. My schema simply looks like -
class MySchema(Schema):
id = fields.Integer()
name = fields.Str()
category = fields.Str()
So in this situation, how and where would I set ordered = True? Thanks!
I solved the issue by changing my schema class to -
class MySchema(Schema):
class Meta:
ordered = True
id = fields.Integer()
name = fields.Str()
category = fields.Str()
and then also adding JSON_SORT_KEYS=False to my app's config.py file.

Querying GenericRelation field DJango

models.py
class Contact(models.Model):
attributes = GenericRelation(
AttributeValue,related_query_name='contact',content_type_field='target_content_type',object_id_field='target_object_id')
class AttributeValue(models.Model):
attribute = models.ForeignKey(Attribute, related_name="attribute")
# the object instance we are associating this attribute to
target_content_type = models.ForeignKey(ContentType, related_name="attribute_value_target_content_type",
on_delete=models.CASCADE, blank=True, null=True)
target_object_id = models.PositiveIntegerField(blank=True, null=True, db_index=True)
target = GenericForeignKey('target_content_type', 'target_object_id')
class Attribute(models.Model):
name = CHarFIeld()
I want to get all the contacts that follow both the conditions below
Have an AttributeValue model object in attributes field with
attributes__attribute__name = 'crop_name' and
attributes__value='groundnut'
Have an AttributeValue model object in attributes field with attributes__attribute__name = 'crop_season' and attributes__value='winter'
One way to achieve this is mentioned as follows:
Contact.objects.all().filter(Q(attributes__attribute__name='crop_name') & Q(attributes__value='groundnut')).filter(Q(attributes__attribute__name='crop_season') & Q(attributes__value='winter'))
The above mentioned query works fine. But I want to construct a single such query that can be given into .filter()
i.e. something like
Contact.objects.filter(query)
I tried doing something like
Contact.objects.filter(Q(Q(attributes__attribute__name='crop_name') & Q(attributes__value='groundnut'))& Q(Q(attributes__attribute__name='crop_season') & Q(attributes__value='winter')))
Expecting that query would respect the braces, but it doesn't like that i guess
P.S. I am using django(1.11.4) + postgres(9.5.7) combination

Writeable Nested Serializers with PrimaryKeyRelatedField

I'm having a problem creating (nested) serializers for the following models:
class Chapter(Model):
uuid = UUIDField(primary_key=True, editable=False)
description = TextField(blank=True)
class Section(Model):
uuid = UUIDField(primary_key=True, editable=False)
description = TextField(blank=True)
chapter = ForeignKey(Chapter, related_name='sections', on_delete=CASCADE)
Here is the serializer for the Chapter model with the create method:
class ChapterSerializer(ModelSerializer):
uuid = UUIDField(read_only=False)
sections = SectionSerializer(many=True, read_only=False)
class Meta:
model = Chapter
fields = ('uuid', 'description', 'sections')
def create(self, validated_data):
sections = validated_data.pop('sections', [])
chapter = Chapter.objects.create(**validated_data)
for section in sections:
section.update({'chapter': chapter})
section = Section.objects.create(**section)
return chapter
And here are two slightly different variants of the Section serializer:
class SectionSerializer(ModelSerializer):
uuid = UUIDField(read_only=False)
chapter = PrimaryKeyRelatedField(read_only=true)
class Meta:
model = Section
fields = ('uuid', 'description', 'chapter')
class SectionSerializer(ModelSerializer):
uuid = UUIDField(read_only=False)
chapter = PrimaryKeyRelatedField(queryset=Chapter.objects.all())
class Meta:
model = Section
fields = ('uuid', 'description', 'chapter')
Now if I try to create a new Chapter with nested Sections and the PrimaryKeyRelatedField in the Section serializer has the queryset parameter set, I get the following error:
'sections': [{'chapter': ['Invalid pk "x" - object does not exist.']}]
If I use the variant with the read_only=true parameter, creating a new Chapter with nested Sections works, but now I can no longer create a new Section (Chapter field is set to an existing Chapter UUID) because the chapter field is removed while validating the input data.
django.db.utils.IntegrityError: null value in column "chapter_id" violates not-null constraint
I think I could solve this by duplicating the SectionSerializer, but this seems to be a very crude solution ...
Anyone knowing of an better approach?
OK, answering my own question:
I finally found a way to get a "nestable" and a "writeable" version of the SectionSerializer without code duplication:
class NestedSectionSerializer(ModelSerializer):
uuid = UUIDField(read_only=False)
class Meta:
model = Section
exclude = ('chapter', )
class SectionSerializer(NestedSectionSerializer):
url = serializers.HyperlinkedIdentityField(view_name='section-detail')
class Meta:
model = Section
fields = '__all__'
class ChapterSerializer(ModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='chapter-detail')
uuid = UUIDField(read_only=False)
sections = NestedSectionSerializer(many=True, read_only=False)
class Meta:
model = Chapter
fields = '__all__'
def create(self, validated_data):
sections = validated_data.pop('sections', [])
chapter = Chapter.objects.create(**validated_data)
for section in sections:
section.update({'chapter': chapter})
section = Section.objects.create(**section)
return chapter
The key here ist to explicitly exclude the reverence to chapter in the "nested" serializer but add it again in the "writeable" serializer. This even scales to deeply nested structures!
And by using inheritance I can avoid most code duplication.
I just pulled my hair about this very same question.
The answer is actually in the DRF documentation: you need to explicitely define the queryset argument for the PrimaryKeyRelatedField if you want them to be writable.
In your case, only the second variant of SectionSerializer is necessary.