How to create object with Foreign Key - json

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 .

Related

How to serialize json to sql?

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

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']

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)

Serialize model with foreign key Django

I have the following json:
The problem with it is the 'tipo_envase' field, whose id is being returned in my json, but I do not want the id, but the whole object that is linked to tipo_envase, which is basically this json:
I tried to serialize the models this way
class TipoEnvaseSerializer(serializers.ModelSerializer):
class Meta:
model = Tipoenvase
fields = ('id','nombre')
class PresentationSerializer(serializers.ModelSerializer):
class Meta:
model = Presentation
fields = ('nombre','capacidad','tipo_envase')
And the models are these:
class Presentation(models.Model):
nombre = models.CharField(max_length=100)
capacidad = models.CharField(max_length=100)
tipo_envase = models.ForeignKey('Tipoenvase', on_delete=models.CASCADE)
def __str__(self):
return self.nombre + " " + self.capacidad + " " + self.tipo_envase.nombre
class Tipoenvase(models.Model):
nombre = models.CharField(max_length=100)
def __str__(self):
return self.nombre
In summary the following json structure is required:
`{
"nombre":"Frasco"
"capacidad":"410 gr"
"tipo_envase":{
"id":"1"
"nombre":"vidrio"
}
}`
You can nest the serializers:
class PresentationSerializer(serializers.ModelSerializer):
tipo_envase = TipoEnvaseSerializer()
class Meta:
model = Presentation
fields = ('nombre','capacidad','tipo_envase')

Return only funded fields in response(Django Rest SearchFilter)

I have ViewSet which have SearchFilter in filter_backends and two search_fields. How to return JSON represents the only field results?
For example if i send response for search with "jo" substring i will have only one field for this search(funded user name or full name), not both user name and email in each JSON object:
{
[{
"username": "jonh";
},
{
"fullname": "Jonh";
},
{
"username": "jo";
},
{
"fullname": "Johnson";
}
]
}
viewsets.py:
class UserViewSet(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
queryset = User.objects.all()
permission_classes = (IsUserOrReadOnly,)
filter_backends = (filters.SearchFilter,)
search_fields = ('^username', '^fullname',)
def get_serializer_class(self):
if hasattr(self, 'action') and self.action == 'list':
return UserListSerializer
return UserDetailSerializer
serializers.py:
class UserListSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'username', 'email', 'fullname')
I would create a Serializer factory function that takes the search params as an an argument:
def generate_user_list_serializer(search_terms):
class UserListSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = tuple(search_terms)
return UserListSerializer
Then in your view, have the serializer generated on the fly:
def get_serializer_class(self):
# make sure that the fields are available
validated_params = [p for p in self.query_params if p in Myfields]
return generate_user_list_serializer(self.query_params)