Custom serializing action with DjangoRestFramework - python

I am having some problem with djangorestframework's serializers.
Below is part of my code.
from django.db import models
from django.contrib.auth.models import User
class Image(models.Model):
id = models.AutoField(primary_key=True)
owner = models.ForeignKey(User)
album = models.ForeignKey(Album, null=True)
name = models.CharField(max_length=128, default='')
class Album(models.Model):
id = models.AutoField(primary_key=True)
owner = models.ForeignKey(User)
name = models.CharField(max_length=128, default='')
class ImageSerializer(serializers.ModelSerializer):
owner = serializers.Field(source='owner.id')
album = serializers.Field(source='album.id')
uploadDevice = serializers.Field(source='uploadDevice.id')
class Meta:
model = Image
fields = ('id', 'owner', 'album', 'name')
class AlbumSerializer(serializers.ModelSerializer):
owner = serializers.Field(source='owner.id')
class Meta:
model = Album
fields = ('id', 'owner', 'name')
So the thing is, I want to hide the integer id value of the models when they are serialized into json. For example, Image model instance with values
id = 12
owner = 425
album = 24
name = DSC2091.JPG
will turn into
{
id: '7VHXHIGMH4XWAKYMPSYYYENYA7NPZ7RGVY6GQJMG3BSIQXWZELNQ====',
owner: 'J2M5BVZB2RCJQNXPN33G2LTMFSAXWPFVFHFTNQSHP56QO3OHFCNA====',
album: 'HCMOMOEEA7YZEI5JJTTXN7LQHOEW3FCRQ7OB6ZMD7UEWISUG7PFA====',
name: 'DSC2091.JPG'
}
And same in the other way.
I've already made the encrypting, decrypting, and the hashing part.
Where and what should I do to make this id security conversion happen smoothly?

Checkout transform methods on serialization: transform_ http://www.django-rest-framework.org/api-guide/serializers/#serializing-objects
Also validate method does the reverse so you can do the encryption going each way.

Related

Django error while serializing image model of child field

I am new to this tech, while working on django project i got some issues when i try to serialize Ticket's account.profile_pic
models.py
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE
profile_pic = models.ImageField(upload_to='images/profile_pics/', blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
class Ticket(models.Model):
author = models.ForeignKey(Account, on_delete=models.CASCADE)
descr = models.TextField(blank=False, null=False)
likes = models.ManyToManyField(User)
serializers.py
class DetailedTicketSerializer(serializers.ModelSerializer):
# Error occurs on below line: No file associated with ImageField
author_profile_pic = serializers.ReadOnlyField(source='author.profile_pic')
author_username = serializers.ReadOnlyField(source='author.user.username')
class Meta:
model = Ticket
fields = ['id', 'author_profile_pic', 'author_username', 'likes', 'descr']
Anyone knows how do i serialize Account.profile_pic's url???
serialize the account class. in your ticketserializer call the account serializer.
Here an example:
class
HobbySerializer(serializers.ModelSerializer):
class Meta:
model = Hobby
fields = '__all__'
class ProfileSerializer(serializers.ModelSerializer):
user_hobby = HobbySerializer(many=True)
class Meta:
model = Profile
fields = '__all__'

How to access reversed relationship using Django rest framework

Here are my models :
class Profile(models.Model):
user = models.ForeignKey(User, related_name="profile", on_delete=PROTECT)
plan = models.ForeignKey(Plans, on_delete=PROTECT)
full_name = models.CharField(max_length=2000)
company_name = models.CharField(max_length=50, null=True, blank=True)
activation_token = models.UUIDField(default=uuid.uuid4)
activated = models.BooleanField(default=False)
thumb = models.ImageField(upload_to='uploads/thumb/', null=True, blank=True)
renew_data = models.DateField()
is_paid = models.BooleanField(default=False)
And as you see the Profile model have user field that is related to the Abstract user of django framework. now here is how i call them using an API :
Serializers
class ProfileSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Profile
fields = ['company_name']
class UserSerializer(serializers.HyperlinkedModelSerializer):
profile_set = ProfileSerializer(
read_only=True, many=True) # many=True is required
class Meta:
model = User
depth = 1
fields = ['username', 'id', 'profile_set']
But when I call the API it shows only the fields username and 'id but not the profile_set
Your UserSerializer should like this,
class UserSerializer(serializers.HyperlinkedModelSerializer):
# no need to set `profile.all` as you have related name profile defined in your model
profile_set = ProfileSerializer(source='profile', many=True)
class Meta:
model = User
depth = 1
fields = ['username', 'id', 'profile_set']
OR,
class UserSerializer(serializers.HyperlinkedModelSerializer):
profile = ProfileSerializer(many=True) # as you have related name `profile`
class Meta:
model = User
depth = 1
fields = ['username', 'id', 'profile']
Try setting the source of your serializer:
profile_set = ProfileSerializer(
source='profile.all',
read_only=True, many=True
)
It looks like you've set the related_name on your foreign key:
user = models.ForeignKey(User, related_name="profile", on_delete=PROTECT)
This defines the reverse relation name, so that's how you need to refer to it in DRF, too:
class UserSerializer(serializers.HyperlinkedModelSerializer):
profile = ProfileSerializer(read_only=True, many=True)
class Meta:
model = User
depth = 1
fields = ['username', 'id', 'profile']
Since it's clearly a plural, I'd also suggest you rename profile to profiles.

Many to Many model with a dropdown in Django Rest Framework?

I am trying to create a Many to Many relation with a model in between, I have a Client model, and a Zone model, each client may have access to different zones, and each zone may have multiple clients.
Therefore I created a model called Access Permission, that stores said relation, and I want to show a dropdown selector in the post form that shows the existing clients and zones, or to ask for the Id of an existing object, instead of showing the form to create new ones.
These are my models:
class Zone(models.Model):
name = models.TextField()
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.name)
class Client(models.Model):
name = models.TextField()
birthDate = models.DateField()
created = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.name)
class AccessPermission(models.Model):
idClient = models.ForeignKey(Client, on_delete=models.CASCADE, null=False)
idZone = models.ForeignKey(Zone, on_delete=models.CASCADE, null=False)
And these my current serializers:
class ZoneSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Zone
fields = ('name',)
class ClientSerializer(serializers.HyperlinkedModelSerializer):
zones = ZonesSerializer(source='accesspermission_set', many=True, read_only=True)
class Meta:
model = Client
fields = ('name', 'birthDate', 'zones')
class AccessPermissionSerializer(serializers.ManyRelatedField):
idClient = ClientSerializer(many=False)
idZone = ZoneSerializer(many=False)
class Meta:
model = AccessPermission
fields = ('idClient', 'idZone')
Is there any way to ask for the Id of an existing object, or show the existing ones, instead of the fields to create new ones?
You can do it like:
models
class AccessPermission(models.Model):
client = models.ForeignKey(Client, on_delete=models.CASCADE, null=False)
zone = models.ForeignKey(Zone, on_delete=models.CASCADE, null=False)
serializers
class AccessPermissionSerializer(serializers.ManyRelatedField):
id = serializers.IntegerField(read_only=True)
client_id = serializers.PrimaryKeyRelatedField(
queryset=Client.objects.all(), source='client', allow_null=False, required=True
)
zone_id = serializers.PrimaryKeyRelatedField(
queryset=Zone.objects.all(), source='zone', allow_null=False, required=True
)
class Meta:
model = AccessPermission
fields = (
'id', 'client_id', 'zone_id'
)

Django Rest Framework: How to do nested serialization with ContentType

I have an application that has a Company, and this company can have 0 or multiple addresses. Companies are not the only 'models' that could have addresses. To achieve this, I use ContentTypes.
models.py
class Company(models.Model):
''' models a company in the system. '''
number = models.CharField(_('Number'), unique=True, max_length=20)
name = models.CharField(_('Name'), max_length=100)
active = models.BooleanField(_('Active'), default=True)
def _get_addresses(self):
'''
'''
contentType = ContentType.objects.get(
model=self.__class__.__name__.lower()
)
try:
addresses = Address.objects.get(
actor_type=contentType, actor_id=self.id
)
except Address.DoesNotExist:
addresses = []
return addresses
addresses = property(_get_addresses)
class Address(Auditable):
''' models an address '''
actor_type = models.ForeignKey(ContentType)
actor_id = models.PositiveIntegerField()
actor_object = fields.GenericForeignKey('actor_type', 'actor_id')
_type = models.CharField(
_('Address Type'),
max_length=10,
choices=sorted(TYPES.items())
)
active = models.BooleanField(default=True)
address1 = models.CharField(_('Address 1'), max_length=50)
address2 = models.CharField(
_('Address 2'),
max_length=50,
...
This way, I could also have a Profile model and I could link multiple addresses to a Profile. However, my problem comes when I tried to implement the Serializers.
Serializers.py
class AddressSerializer(serializers.ModelSerializer):
class Meta:
model = Address
fields = (
'_type',
'address1',
'address2',
'address3',
'country',
'state',
'city'
)
class CompanySerializer(serializers.ModelSerializer):
addresses = AddressSerializer(required=False, many=True)
class Meta:
model = Company
fields = (
'number',
'name',
'addresses'
)
This implementation gives me this error. It says that it cannot iterate through Address model (which makes sense), but I'm not sure how to make my addresses iterable.
I would need to perform CRUD operations not only on the Company, but also on the nested Addresses.
Any suggestions/ideas on how to go about this?
The following database schema is just a suggestion for the scenario prevailed.
class Details(models.Model):
official_name = models.CharField(....)
is_company = models.BooleanField(default=False)
#something like that....
class Address(models.Model):
owner = models.ForeignKey(Details, related_name='addresses')
is_company = models.BooleanField(default=False)
building_name = .....
building_no = ......
locality = ......
#some other fields.....
class Company(models.Model):
details = models.OneToOneField(Details, related_name='company')
name = models.CharField(...)
number = .....
is_active = ........
class Profile(models.Model):
details = models.OneToOneField(Details, related_name='profile')
name = models.CharField(....)
.........
Here, each Company table and Profile table has a one to one relationship with a Details table. The Details table is related to the Address table with a Foreign key in it. So, each Company or Profile can have multiple addresses through the Details table.
So, the queries would be like,
For accessing addresses from the Company or Profile instances,
company.details.addresses.all()#or
profile.details.addresses.all()
For reverse queries are simple as the tables contains the respective fields in them, for a given address the owner would be, address.owner.profile or address.owner.company which could be determined by a flag in the respective tables.
I know, designing the database like this is somewhat tiring. But, this does helps in serialization of the data into a better format.
Serializers can be as follows,
class DetailsSerializer(ModelSerializer):
addresses = AddressSerializer(source='addresses', many=True)
class Meta:
model = Details
fields = ('addresses',)
class AddressSerializer(ModelSerializer):
class Meta:
model = Address
fields = '__all__'
class CompanySerializer(ModelSerializer):
details = DetailsSerializer()
class Meta:
model = Company
fields = ('details', .........)
class ProfileSerializer(ModelSerializer):
details = DetailsSerializer()
class Meta:
model = Profile
fields = ('details', .........)

django rest serialize level 2 field

Hi I need to add a field in a serializer of a 2 level reference item.
I have the following model:
model.py:
class Company(models.Model):
companyName = models.CharField(max_length=50, blank=True)
class Poll(models.Model):
questionString = models.CharField(max_length=500, blank=True)
companyId = models.ForeignKey(Company, null=True, db_column='companyId', blank=True)
class PossibleAnswer(models.Model):
answerString = models.CharField(max_length=100, blank=True)
pollId = models.ForeignKey(Poll, null=True, db_column='pollId', blank=True,related_name='answers')
token = models.CharField(max_length=10, blank=True)
serializers.py:
class PossibleAnswerSerializer(serializers.ModelSerializer):
#companyId = serializers.RelatedField()
class Meta:
model = PossibleAnswer
fields = ('answerString', 'token', 'pollId', 'companyId')
I want to make a Serializer for the PossibleAnswer object that has a field named company. How to make a this reference? Something similar to: pollId__companyId in a django query set filter.
Another solution...
class PossibleAnswerSerializer(serializers.ModelSerializer):
companyId = serializers.SerializerMethodField()
def get_companyId(self, obj):
return obj.pollId.companyId
class Meta:
model = PossibleAnswer
fields = ('answerString', 'token', 'pollId', 'companyId',)
I the field is read-only you can easily achieve this with a serializers.Field, which accept dotted paths to the source.
Your Serializer would be:
class PossibleAnswerSerializer(serializers.ModelSerializer):
companyId = serializers.Field(source='pollId.companyId')
class Meta:
model = PossibleAnswer
fields = ('answerString', 'token', 'pollId', 'companyId')
I too agree with Erik, that naming model attributes with Id is a bad idea even though the DB representation is only the ID.

Categories

Resources