django-rest-framework

Nested serializer “Through model” in Django Rest Framework

你离开我真会死。 提交于 2021-02-08 04:33:32
问题 I am having difficulty serializing an intermediary "pivot" model and attach to each item in an Many-to-many relation in Django Rest Framework. Example: models.py: 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

Nested serializer “Through model” in Django Rest Framework

淺唱寂寞╮ 提交于 2021-02-08 04:33:09
问题 I am having difficulty serializing an intermediary "pivot" model and attach to each item in an Many-to-many relation in Django Rest Framework. Example: models.py: 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

DRF how to save a model with a foreign key to the User object

南笙酒味 提交于 2021-02-07 20:00:23
问题 I have the following model: class Journal(models.Model): project_name = models.TextField() intro = models.TextField(blank=True) start_time = models.DateTimeField() end_time = models.DateTimeField() creator = models.ForeignKey(User) group = models.OneToOneField(Group) contact_info = models.TextField(blank=True) allow_anon = models.BooleanField(default=True) created = models.DateTimeField(auto_now_add=True) I would like to save a record of this model when users submit a form. The values of all

DRF how to save a model with a foreign key to the User object

久未见 提交于 2021-02-07 19:59:20
问题 I have the following model: class Journal(models.Model): project_name = models.TextField() intro = models.TextField(blank=True) start_time = models.DateTimeField() end_time = models.DateTimeField() creator = models.ForeignKey(User) group = models.OneToOneField(Group) contact_info = models.TextField(blank=True) allow_anon = models.BooleanField(default=True) created = models.DateTimeField(auto_now_add=True) I would like to save a record of this model when users submit a form. The values of all

Django Rest Framework metrics per user requests

情到浓时终转凉″ 提交于 2021-02-07 19:16:56
问题 I'm currently working on an Api build with Django Rest Framework, uwsgi nginx and memcached. I would like to know what is the best way to get users statistics like number of requests per user? Taking in consideration that the infrastructure is probably going to scale to multiple servers. And is there is a way to determine if the response was retrieve from cache or from the application? What I'm thinking is processing the Nginx logs to separate the request by user and apply all the

'collections.OrderedDict' object has no attribute 'pk' - django rest framework

北战南征 提交于 2021-02-07 19:10:37
问题 I have a model and I want to write an update() method for it in order to update. The below snippet is my model: class Klass(models.Model): title = models.CharField(max_length=50) description = models.CharField(max_length=500) university = models.CharField(max_length=50,blank=True, null=True) teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE) and the below snippet is corresponding Serializer : class KlassSerializer(ModelSerializer): teacher =

'collections.OrderedDict' object has no attribute 'pk' - django rest framework

旧巷老猫 提交于 2021-02-07 19:09:24
问题 I have a model and I want to write an update() method for it in order to update. The below snippet is my model: class Klass(models.Model): title = models.CharField(max_length=50) description = models.CharField(max_length=500) university = models.CharField(max_length=50,blank=True, null=True) teacher = models.ForeignKey(Profile, related_name='teacher', on_delete=models.CASCADE) and the below snippet is corresponding Serializer : class KlassSerializer(ModelSerializer): teacher =

How to get custom list view in django-rest-framework viewset

旧时模样 提交于 2021-02-07 11:54:32
问题 I have a tv channel model and created a django-restframework viewlet which gives me a list and a detail view out of the box. On top I added two custom single-object views called all_events and now_and_next_event, as described here: Marking extra methods for routing. That works great so far. class ChannelViewSet(viewsets.ModelViewSet): """ A viewset for viewing and editing channel instances. """ serializer_class = serializers.ChannelSerializer queryset = Channel.objects.all() @link() def now

How do I create a serializer that reuses a unique key of my model?

牧云@^-^@ 提交于 2021-02-07 10:36:56
问题 I'm using Python 3.7, Django 2.2, the Django rest framework, and pytest. I have the following model, in which I want to re-use an existing model if it exists by its unique key ... class CoopTypeManager(models.Manager): def get_by_natural_key(self, name): return self.get_or_create(name=name)[0] class CoopType(models.Model): name = models.CharField(max_length=200, null=False, unique=True) objects = CoopTypeManager() Then I have created the below serializer to generate this model from REST data

How to add search parameters to GET request in Django REST Framework?

放肆的年华 提交于 2021-02-07 09:21:58
问题 After having read through and completed the Django REST Framework tutorial, it is not completely obvious how one would implement a filter on a GET request. For example, ListAPIView is great for viewing all the instances of a Model in your database. However, if I wanted to limit the results (e.g. for a Book model, I might want to limit the results by publication date or author, etc.). It seems the best way to do this would be to create a custom Serializer, View, etc. and basically write