👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I have a question about dealing with m2m / through models and their presentation in django rest framework. Let"s take a classic example:
models.py:
from django.db import models
class Member(models.Model):
name = models.CharField(max_length = 20)
groups = models.ManyToManyField("Group", through = "Membership")
class Group(models.Model):
name = models.CharField(max_length = 20)
class Membership(models.Model):
member = models.ForeignKey("Member")
group = models.ForeignKey("Group")
join_date = models.DateTimeField()
serializers.py:
imports...
class MemberSerializer(ModelSerializer):
class Meta:
model = Member
class GroupSerializer(ModelSerializer):
class Meta:
model = Group
views.py:
imports...
class MemberViewSet(ModelViewSet):
queryset = Member.objects.all()
serializer_class = MemberSerializer
class GroupViewSet(ModelViewSet):
queryset = Group.objects.all()
serializer_class = GroupSerializer
When GETing an instance of Member, I successfully receive all of the member"s fields and also its groups - however I only get the groups" details, without extra details that comes from the Membership model.
In other words I expect to receive:
{
"id" : 2,
"name" : "some member",
"groups" : [
{
"id" : 55,
"name" : "group 1"
"join_date" : 34151564
},
{
"id" : 56,
"name" : "group 2"
"join_date" : 11200299
}
]
}
Note the join_date.
I have tried oh so many solutions, including of course Django Rest-Framework official page about it and no one seems to give a proper plain answer about it - what do I need to do to include these extra fields? I found it more straight-forward with django-tastypie but had some other problems and prefer rest-framework.
👻 Read also: what is the best laptop for engineering students?
Include intermediary (through model) in responses in Django Rest Framework __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Include intermediary (through model) in responses in Django Rest Framework __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Include intermediary (through model) in responses in Django Rest Framework, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Include intermediary (through model) in responses in Django Rest Framework
- Deutsch Include intermediary (through model) in responses in Django Rest Framework
- Français Include intermediary (through model) in responses in Django Rest Framework
- Español Include intermediary (through model) in responses in Django Rest Framework
- Türk Include intermediary (through model) in responses in Django Rest Framework
- Русский Include intermediary (through model) in responses in Django Rest Framework
- Português Include intermediary (through model) in responses in Django Rest Framework
- Polski Include intermediary (through model) in responses in Django Rest Framework
- Nederlandse Include intermediary (through model) in responses in Django Rest Framework
- 中文 Include intermediary (through model) in responses in Django Rest Framework
- 한국어 Include intermediary (through model) in responses in Django Rest Framework
- 日本語 Include intermediary (through model) in responses in Django Rest Framework
- हिन्दी Include intermediary (through model) in responses in Django Rest Framework
Warsaw | 2023-03-29
Maybe there are another answers? What Include intermediary (through model) in responses in Django Rest Framework exactly means?. Checked yesterday, it works!
Prague | 2023-03-29
Thanks for explaining! I was stuck with Include intermediary (through model) in responses in Django Rest Framework for some hours, finally got it done 🤗. Checked yesterday, it works!
Berlin | 2023-03-29
Maybe there are another answers? What Include intermediary (through model) in responses in Django Rest Framework exactly means?. I just hope that will not emerge anymore