django-rest-framework

ModuleNotFoundError: No module named 'rest_framework'

别说谁变了你拦得住时间么 提交于 2021-01-05 09:15:07
问题 I'm trying to follow a tutorial on DRF, but when I'm about to run "migrate" for the database, i get ModuleNotFoundError: No module named 'rest_framework'. As PyCharm hints, the same also applies to django_summernote and djoser I have there. I know there are some threads like this, but nothing from those seems to help - the Python console DOES recognize these modules, and they are added through INSTALLED_APPS INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib

How to post OneToOne field in django rest-framework using overwrite create method

落爺英雄遲暮 提交于 2021-01-05 07:31:15
问题 I am trying to override create method to make a post request but i am stuck, here is my code class Order(models.Model): street_number = models.PositiveIntegerField(blank=True, null=True) street_name = models.CharField(max_length=250, null=True, blank=True) class Seller(models.Model): order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='seller_order',blank=True, null=True) first_name = models.CharField(max_length=250, null=True, blank=True) last_name = models.CharField

Serialize ManyToManyFields with a Through Model in Django REST Framework

天涯浪子 提交于 2021-01-05 07:26:05
问题 I have this M2M relation with through model as class Person(models.Model): name = models.CharField(max_length=128) def __str__(self): return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') def __str__(self): return self.name class Membership(models.Model): person = models.ForeignKey(Person, on_delete=models.CASCADE) group = models.ForeignKey(Group, on_delete=models.CASCADE) date_joined = models

Serialize ManyToManyFields with a Through Model in Django REST Framework

最后都变了- 提交于 2021-01-05 07:25:25
问题 I have this M2M relation with through model as class Person(models.Model): name = models.CharField(max_length=128) def __str__(self): return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') def __str__(self): return self.name class Membership(models.Model): person = models.ForeignKey(Person, on_delete=models.CASCADE) group = models.ForeignKey(Group, on_delete=models.CASCADE) date_joined = models

How to post OneToOne field in django rest-framework using overwrite create method

允我心安 提交于 2021-01-05 07:24:24
问题 I am trying to override create method to make a post request but i am stuck, here is my code class Order(models.Model): street_number = models.PositiveIntegerField(blank=True, null=True) street_name = models.CharField(max_length=250, null=True, blank=True) class Seller(models.Model): order = models.OneToOneField(Order, on_delete=models.CASCADE, related_name='seller_order',blank=True, null=True) first_name = models.CharField(max_length=250, null=True, blank=True) last_name = models.CharField

How to handle an exception in a Django Middleware?

一曲冷凌霜 提交于 2021-01-05 07:03:58
问题 I have a problem with proper handling an exception in Django middleware. My exception: from rest_framework.exceptions import APIException from rest_framework.status import HTTP_403_FORBIDDEN class MyProfileAuthorizationError(APIException): def __init__(self, msg): APIException.__init__(self, msg) self.status_code = HTTP_403_FORBIDDEN self.message = msg And my Middleware: class PatchRequestUserWithProfile: def __init__(self, get_response): self.get_response = get_response def __call__(self,

DRF PrimaryRelatedField when write and NestedSerializer when read?

两盒软妹~` 提交于 2021-01-05 06:40:34
问题 I am using a nested serializer. I need ProfileSerializer to return full related Project object for get requests and consider only id switching (changing current) like with relatedPrimaryField behaiviour for post/put requests on ProfileSerializer . any solutions on how to achieve this ? class ProfileSerializer(serializers.ModelSerializer): current = ProjectSerializer() class Meta: model = Profile fields = ('function', 'current') 回答1: As Linova mentioned, the easiest way to solve this issue

How to update __str__ and slug everytime after Django's model update?

随声附和 提交于 2021-01-05 04:45:41
问题 I have created this model in Django which also saves date and slug. But even if I am making these changes again the str and the slug still remains the same and not updating as per the new data. How to fix this ? class EntranceExamination(models.Model): course = models.CharField(max_length=100, blank=True, default='') year = models.PositiveIntegerField(choices=year_choices(),default=current_year(), validators=[MinValueValidator(1984), max_value_current_year]) month = models

How to update __str__ and slug everytime after Django's model update?

為{幸葍}努か 提交于 2021-01-05 04:43:11
问题 I have created this model in Django which also saves date and slug. But even if I am making these changes again the str and the slug still remains the same and not updating as per the new data. How to fix this ? class EntranceExamination(models.Model): course = models.CharField(max_length=100, blank=True, default='') year = models.PositiveIntegerField(choices=year_choices(),default=current_year(), validators=[MinValueValidator(1984), max_value_current_year]) month = models

Django ForeignKey field required despite blank=True and null=True

我只是一个虾纸丫 提交于 2021-01-04 05:31:42
问题 I am using Django REST Framework and I have a MyNodel with a related MyOtherModel in a many-to-one relationship: models.ForeignKey(MyModel, related_name="my_other_models", blank=True, null=True) Although blank=True, null=True , when I try to post a MyModel JSON without a my_other_models field I get a "this field is required" error. 回答1: In your serializer, you need to add required=False . field = MyModelSerializer(required=False) 来源: https://stackoverflow.com/questions/40237969/django