How to serialize json to sql? - json

I have a list of words and I should send requests to an API and get the information about the words. I want to convert the API data which is in JSON format to SQL(my DB is PostgreSQL) format in Django. How can I do that? Do you know any good source to learn to serialize json to sql?
I have just started learning Django.
It is the API's JSON data:
[
{
"word": "hello",
"phonetics": [
{
"text": "/həˈloʊ/",
"audio": "https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3"
},
{
"text": "/hɛˈloʊ/",
"audio": "https://lex-audio.useremarkable.com/mp3/hello_us_2_rr.mp3"
}
],
"meanings": [
{
"partOfSpeech": "exclamation",
"definitions": [
{
"definition": "Used as a greeting or to begin a phone conversation.",
"example": "hello there, Katie!"
}
]
},
{
"partOfSpeech": "noun",
"definitions": [
{
"definition": "An utterance of “hello”; a greeting.",
"example": "she was getting polite nods and hellos from people",
"synonyms": [
"greeting",
"welcome",
"salutation",
"saluting",
"hailing",
"address",
"hello",
"hallo"
]
}
]
},
{
"partOfSpeech": "intransitive verb",
"definitions": [
{
"definition": "Say or shout “hello”; greet someone.",
"example": "I pressed the phone button and helloed"
}
]
}
]
}
]
this is my models.py:
class Words(models.Model):
word = models.CharField(max_length=50)
american_phonetic= models.CharField(max_length=50)
american_audio= models.URLField(max_length = 200)
british_phonetic= models.CharField(max_length=50)
british_audio= models.URLField(max_length = 200)
###########################################################################
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
birth_date = models.DateField('birth date')
field= models.CharField(max_length=50)
location = models.CharField(max_length=30, blank=True)
interest= models.IntegerField() # for example : 1 for science , 2 for art , 3 for sport etc.
education= models.IntegerField() # for example : 1 for highschool , 2 for bachelor , 3 for master and 4 for phd
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
class UserLearned(models.Model):
Profile_id = models.ForeignKey(Profile, on_delete=models.CASCADE)
word_id = models.ForeignKey(Words, models.SET_NULL, blank=True, null=True,)
def __str__(self):
return self.word_id
############################################################################
class Meanings(models.Model):
word_id = models.ForeignKey(Words, on_delete=models.CASCADE)
partOfSpeech = models.CharField(max_length=30)
class Definitions(models.Model):
word_id = models.ForeignKey(Words, on_delete=models.CASCADE)
Meaning_id = models.OneToOneField(Meanings, on_delete=models.CASCADE, primary_key=True)
definition = models.TextField()
def __str__(self):
return self.definition
class Examples(models.Model):
word_id = models.ForeignKey(Words, on_delete=models.CASCADE)
Meaning_id = models.OneToOneField(Meanings, on_delete=models.CASCADE, primary_key=True)
example = models.TextField()
def __str__(self):
return self.example
class Synonyms(models.Model):
word_id = models.ForeignKey(Words, on_delete=models.CASCADE)
Meaning_id = models.ForeignKey(Meanings, on_delete=models.CASCADE)
synonym = models.CharField(max_length=50)
def __str__(self):
return self.synonym

You can use the json library to serialise the json data and vice versa (use json.loads() & json.dumps() methods).
For Example:
import json
data = "{'name': 'Jack', 'age': 30}" #json data
python_dict = json.loads(data) #now it is a python dict
Now you can directly assign the values of the python dict to any model attributes in Django.
Following are some good sources to start learning the library:
https://www.w3schools.com/python/python_json.asp
https://docs.python.org/3/library/json.html

Related

Serialize the models to make them correspond to a json data format

I have three models:
class GWS(models.Model):
id = models.IntegerField(primary_key=True)
rssi = models.FloatField()
snr = models.FloatField()
class Data(models.Model):
gwid = models.CharField(max_length=20)
rssi = models.FloatField()
snr = models.FloatField()
freq = models.FloatField()
dr = models.IntegerField()
adr = models.BooleanField()
classe = models.CharField(max_length=2,verbose_name="class")
fCnt = models.IntegerField()
fPor = models.IntegerField()
confirmed = models.BooleanField()
data = models.CharField(max_length=100)
gws = models.ManyToManyField(GWS,related_name="donnees")
class DataUplink(models.Model):
appID = models.IntegerField(primary_key=True)
type = models.CharField(max_length=10)
time = models.IntegerField()
data = models.ForeignKey(Data,on_delete=models.DO_NOTHING)
The following json format represents the data of the DataUplink model sent from a third party application.
{
"devEUI": "8cf9572000023509",
"appID": 1,
"type": "uplink",
"time": 1629378939869,
"data": {
"gwid": "b827ebfffebce2d3",
"rssi": -77,
"snr": 10,
"freq": 868.1,
"dr": 5,
"adr": true,
"class": "C",
"fCnt": 852,
"fPort": 8,
"confirmed": false,
"data": "AgUCIzYBAIERAAAAAAAAAAAAAAAAAAAAAAAAAADHDgA=",
"gws": [
{
"id": "b827ebfffebce2d3",
"rssi": -77,
"snr": 10
}
]
}
}
I am writing an api which must retrieve this data in json via a post request and save it in the database. My question is how do I serialize my models?
below what i did but that doesn't work:
class DataSerializer(serializers.ModelSerializer):
class Meta:
model = Data
fields = '__all__'
class UplinkSerializer(serializers.ModelSerializer):
class Meta:
model = Uplink
fields = '__all__'
class GWSSerializer(serializers.ModelSerializer):
class Meta:
model = GWS
fields = '__all__'

Return nested JSON from Models with relations in Django

models.py (simplified)
class Author(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
def get_books(self):
return Book.objects.filter(author=self.pk)
class Book(models.Model):
name = models.CharField(max_length=255)
pages = models.IntegerField()
author = models.ForeignKey(Author, on_delete=models.CASCADE)
def __str__(self):
return f'{self.name} from {self.author}'
class Paragraph(models.Model):
name = models.CharField(max_length=255)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
def __str__(self):
return f'{self.name} from {self.book}'
I want to return all the instances in a json file with this structure:
[
{
"name": 'Dumas',
"books": {
"name": "The count of Montecristo",
"paragraphs": {
"name": "paragraph_name_1",
},
{
"name": "paragraph_name_2",
},
{
"name": "The three Musketeers",
"paragraphs": {
"name": "paragraph_name",
},
]
What I tried:
serializers.py
class AuthorSerializer(serializers.ModelSerializer):
books = serializers.CharField(source='get_books', read_only=True)
class Meta:
model = Author
fields = ['name', 'books']
This add the books key but the value is the string representation of the istances of Book (of course), how I make the value being the serialized istances of Book? I have created a BookSerializer.
Notes:
I know that I can created a nested json by creating a serializer for Paragraph with depth = 2 but this will include fields I don't want (like pages in Book) and the json structure will be totally different.
You can create nested serializer as
class ParagraphSerializer(serializers.ModelSerializer):
class Meta:
model = Paragraph
fields = ("name",)
class BookSerializer(serializers.ModelSerializer):
paragraphs = ParagraphSerializer(source="paragraph_set", many=True, read_only=True)
class Meta:
model = Book
fields = ("name", "paragraphs")
class AuthorSerializer(serializers.ModelSerializer):
books = BookSerializer(source="book_set", read_only=True, many=True)
class Meta:
model = Author
fields = ['name', 'books']

Convert PointFeild into JSON object

Is there any way in which I can serialize POINTFEILD of GeoDjango into JSON?
I have following model
class Company(models.Model):
name = models.CharField(max_length=200, default='Company', null=True)
def __unicode__(self):
return self.name
class Shop(models.Model):
name = models.CharField(max_length=200, default="bla")
address = models.CharField(max_length=300, default='blabla')
location = models.PointField(null=True, blank=True, geography=True)
company = models.ForeignKey(
Company, on_delete=models.CASCADE, null=True)
and following serializer
class ShopSerializer(serializers.ModelSerializer):
distance = serializers.DecimalField(
source='distance.km', max_digits=10, decimal_places=2, required=False, read_only=True)
# serialize('geojson', Shop.objects.all(),
# geometry_field='location', fields=('name', 'address'))
class Meta:
model = Shop
fields = ['id', 'name', 'address', 'location', 'distance']
class CompanySerializer(serializers.ModelSerializer):
shop_set = ShopSerializer(many=True)
class Meta:
model = Company
fields = ['id', 'name', 'shop_set']
def create(self, validated_data):
shop_validated_data = validated_data.pop('shop_set')
company = Company.objects.create(**validated_data)
shop_set_serializer = self.fields['shop_set']
for each in shop_validated_data:
each['company'] = company
shops = shop_set_serializer.create(shop_validated_data)
return company
Using GeoJson I can get the response as follows
[
{
"id": 1,
"name": "Cosmetica",
"address": "somwhere",
"location": {
"type": "Point",
"coordinates": [
24.896,
67.182
]
}
},
but here the point field has converted into an array. But I need it to be Json object with 2 key-value pairs as
location:{lat,long}
can anyone help please
Why don't u use geos API?
https://docs.djangoproject.com/en/3.1/ref/contrib/gis/geos/#django.contrib.gis.geos.GEOSGeometry.coords
pnt.coords

django rest api json data category subcategory

I am trying to access subcategories according to parent id. When i am checking subcategories api it's showing all data with all parent id. I am unable to filter subcategories according to parent id. I am trying to get json data according to parent id.
If our parent id is 7 so i need all subcategories which one's has parent id 7. Please guide how i can do its.
models.py
class Category(models.Model):
name = models.CharField(max_length=254, unique=True)
status = models.BooleanField(default=True)
def __str__(self):
return self.name
class SubCategory(models.Model):
name = models.CharField(max_length=254, unique=True)
id_parent = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True)
price = models.IntegerField()
status = models.BooleanField(default=True)
def __str__(self):
return self.name
json
[
{
"id": 1,
"name": "IT Servic",
"price": 2000,
"status": true,
"id_parent": 7
},
{
"id": 2,
"name": "Web Development",
"price": 1000,
"status": true,
"id_parent": 8
},
{
"id": 3,
"name": "Digital Marketing",
"price": 3000,
"status": true,
"id_parent": 7
},
{
"id": 4,
"name": "RO Repair",
"price": 3444,
"status": true,
"id_parent": 9
}
]
serializers.py
class CategorySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Category
fields = '__all__'
class SubCategorySerializer(serializers.ModelSerializer):
class Meta:
model = SubCategory
fields = '__all__'
lookup_field = 'id_parent'
views.py*
class CategoryViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
serializer_class = CategorySerializer
def get_queryset(self):
user = self.request.user
if user.is_authenticated:
if user is not None:
if user.is_active and user.is_superuser:
return Category.objects.all()
raise PermissionDenied()
raise PermissionDenied()
raise PermissionDenied()
class SubCategoryViewSet(viewsets.ModelViewSet):
permission_classes = (IsAuthenticated,)
def get_queryset(self):
user = self.request.user
if user.is_authenticated:
if user is not None:
if user.is_active and user.is_superuser:
return SubCategory.objects.all()
raise PermissionDenied()
raise PermissionDenied()
raise PermissionDenied()
serializer_class = SubCategorySerializer
You should take a look at filtering.
Basically, you can either do your filtering "manually" in the get_queryset()
def get_queryset(self):
id_parent = self.request.query_params.get('id_parent')
queryset = super().get_queryset()
if id_parent:
queryset = queryset.filter(id_parent=id_parent)
return queryset
Or use a third-party library such as django-filter
you can add a custom action to your viewset like this, assuming you can set the appropriate urls/routes
#action(detail=False, methods=['GET'])
def get_sub_categories(self, request, *args, **kwargs):
category_id = self.request.query_params.get("category_id", None)
queryset = self.get_queryset()
filtered_sub_categories = queryset.filter(id_parent=category_id)
serializer = self.get_serializer(filtered_sub_categories, many=True)
return Response(serializer.data)

How to create object with Foreign Key

Fisrt,here is my models:
class Question(models.Model):
description = models.CharField(max_length=200)
analysis = models.CharField(max_length=50)
def __str__(self):
return self.description
class QuestionOption(models.Model):
question = models.ForeignKey(Question,related_name='options')
content = models.CharField(max_length=100)
isAnswer = models.BooleanField()
def __str__(self):
return self.question.description + " " + self.content
My Serializers:
class QuestionSerializer(ModelSerializer):
class Meta:
model = Question
fields = '__all__'
The Serializer of QuestionOption is as same
My ViewSet:
class QuestionViewSet(ModelViewSet):
queryset = Question.objects.all()
serializer_class = QuestionRetriveSerilzer
I want to post a Json data,like this:
{
"options": [
{
"content": "This is the first option",
"isAnswer": false
},
{
"content": "This is the second option",
"isAnswer": true
}
],
"description": "which one is true?",
"analysis": "It's esay"
}
I hope my QuestionViewSet can create a Question and two QuestionOption for me automatically,and when I post that Json data,the options is null list,so I override the create method of QuestionViewSet,like this:
def create(self, request, *args, **kwargs):
serializer = QuestionSerializer(data=request.data)
question = serializer.save()
for data in request.data['options']:
data['question'] = question.id
optionSeializer = OptionSerializer(data=data)
print optionSeializer.is_valid()
optionSeializer.save()
return Response(serializer.data,status=status.HTTP_200_OK)
And this method can work,but I want to find a simpler way to do it,cause I must override update and other methods,it's not a easy task...
So how to design Serializers and ViewSet in order to automatically create objects and update objects with foreign key ?
drf-writable-nested may help .