first custom tag seriliazer tests
This commit is contained in:
@@ -16,7 +16,7 @@ This README aims to document functionality of backend as well as required steps
|
||||
From inside the project's folder:
|
||||
- Make migrations:
|
||||
```
|
||||
python manage.py makemigrations core companies products history stats
|
||||
python manage.py makemigrations core geo companies products history stats
|
||||
python migrate
|
||||
```
|
||||
- Start server in development mode: `python manage.py runserver`
|
||||
|
||||
@@ -44,6 +44,7 @@ INSTALLED_APPS = [
|
||||
'rest_framework',
|
||||
'django_filters',
|
||||
'corsheaders',
|
||||
'taggit_serializer',
|
||||
|
||||
# local apps
|
||||
'core',
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from django.contrib.gis.db import models
|
||||
|
||||
from tagulous.models import SingleTagField, TagField
|
||||
|
||||
from companies.models import Company
|
||||
|
||||
# Create your models here.
|
||||
@@ -30,9 +32,9 @@ class Product(models.Model):
|
||||
update_date = models.DateTimeField('Fecha de actualización de producto', null=True, blank=True)
|
||||
discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True)
|
||||
stock = models.PositiveIntegerField('Stock', null=True)
|
||||
# tags = models.ManyToMany(Tag, null=True, blank=True )
|
||||
# category = models.ForeignKey(Tag, null=true) # main tag category
|
||||
# attributes = models.ManyToMany(Tag, null=True, blank=True )
|
||||
tags = TagField( )
|
||||
category = SingleTagField() # main tag category
|
||||
attributes = TagField()
|
||||
identifiers = models.TextField('Identificador único de producto', null=True, blank=True)
|
||||
|
||||
# internal
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer
|
||||
from products.models import Product
|
||||
|
||||
from utils.tag_serializers import SingleTagSerializerField
|
||||
|
||||
|
||||
class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
|
||||
|
||||
tags = TagListSerializerField( )
|
||||
category = SingleTagSerializerField() # main tag category
|
||||
attributes = TagListSerializerField()
|
||||
|
||||
class ProductSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Product
|
||||
exclude = ['created', 'updated', 'creator']
|
||||
|
||||
@@ -6,3 +6,4 @@ django-dotenv==1.4.2
|
||||
django-filter==2.4.0
|
||||
-e git://github.com/darklow/django-suit/@v2#egg=django-suit
|
||||
django-cors-headers==3.5.0
|
||||
django-taggit-serializer==0.1.7
|
||||
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
39
utils/tag_serializers.py
Normal file
39
utils/tag_serializers.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
|
||||
class SingleTag(str):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def __str__(self):
|
||||
return self
|
||||
|
||||
|
||||
class SingleTagSerializerField(serializers.Field):
|
||||
child = serializers.CharField()
|
||||
default_error_messages = {
|
||||
'not_a_str': 'Expected a string but got type "{input_type}".',
|
||||
}
|
||||
order_by = None
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super(SingleTagSerializerField, self).__init__(**kwargs)
|
||||
|
||||
def to_internal_value(self, value):
|
||||
if isinstance(value, str):
|
||||
if not value:
|
||||
value = ""
|
||||
|
||||
if not isinstance(value, str):
|
||||
self.fail('not_a_str', input_type=type(value).__name__)
|
||||
|
||||
return value
|
||||
|
||||
def to_representation(self, value):
|
||||
if not isinstance(value, SingleTag):
|
||||
if not isinstance(value, str):
|
||||
value = value.name
|
||||
value = SingleTag(value)
|
||||
return value
|
||||
Reference in New Issue
Block a user