django-rest-framework

How to pass extra context to Django Rest Framework serializers

百般思念 提交于 2020-06-05 06:34:22
问题 It is my first project using Django rest framework and i'm struggling to get this right. I know that in mainstream Django framework, if i need to add extra contexts to a class-based view, i will do something like this: class PostDetail(DetailView): model = Post def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # Add in a QuerySet of all the books by a certain author John context['john_books'] = Book.objects.filter(author=john) return context then, i will be

Django Rest Framework Doesn't escape Html Entity Problem [closed]

余生长醉 提交于 2020-06-01 07:41:26
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 days ago . I wrote the django blog and api. When i started the postman and then wrote the api url it returns correct values except content i have issue with content content is returning &ouml . It's turkish charset &ouml=ö How can i fix this problem ? api/serializers.py: class MakaleSerializer(serializers

Django Rest Framework Doesn't escape Html Entity Problem [closed]

橙三吉。 提交于 2020-06-01 07:41:12
问题 Closed. This question needs debugging details. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 days ago . I wrote the django blog and api. When i started the postman and then wrote the api url it returns correct values except content i have issue with content content is returning &ouml . It's turkish charset &ouml=ö How can i fix this problem ? api/serializers.py: class MakaleSerializer(serializers

Increasing the count field of model whenever a search is applied

允我心安 提交于 2020-06-01 05:06:28
问题 My model looks like class Article(models.Model): article_type = models.ForeignKey( ArticleType, on_delete=models.CASCADE, related_name='articles' ) title = models.CharField( max_length=100, verbose_name='Article Title' ) count = models.IntegerField( verbose_name='Frequency Count' ) def __str__(self): return self.title And I have a search api class SearchViewSet(RetrieveModelViewSet): serializer_class = ArticleSerializer queryset = Article.objects.all() query = self.request.query_params.get(

NOT NULL constraint failed: api_userlog.browser_info_id (when i want to add show me this error but update and list are working fine)

北城余情 提交于 2020-06-01 04:45:56
问题 model.py from django.db import models from django.utils import timezone Create your models here. class PageOnHold(models.Model): timestamp = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now) spent_time = models.IntegerField(max_length=None,blank=True, null=True) elapsed_time = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now) class MouseClick(models.Model): timestamp = models.DateTimeField(blank=True, null=True,default=timezone.now

NOT NULL constraint failed: api_userlog.browser_info_id (when i want to add show me this error but update and list are working fine)

旧时模样 提交于 2020-06-01 04:45:28
问题 model.py from django.db import models from django.utils import timezone Create your models here. class PageOnHold(models.Model): timestamp = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now) spent_time = models.IntegerField(max_length=None,blank=True, null=True) elapsed_time = models.DateTimeField(max_length=None,blank=True, null=True,default=timezone.now) class MouseClick(models.Model): timestamp = models.DateTimeField(blank=True, null=True,default=timezone.now

Auto-generated field 'user_ptr' clashes with declared field of the same name

余生颓废 提交于 2020-05-29 09:19:06
问题 I have a Django app that has CustomUser. My model looks something like class CustomUser(AbstractBaseUser): def get_short_name(self): pass def get_full_name(self): pass firstName = models.CharField(max_length=300) middleName = models.CharField(max_length=300, blank=True) lastName = models.CharField(max_length=300, blank=True) username = models.CharField(unique=True, max_length=50) businessName = models.CharField(max_length=500, default=None) mobileNumber = models.CharField(max_length=20)

Auto-generated field 'user_ptr' clashes with declared field of the same name

天涯浪子 提交于 2020-05-29 09:18:12
问题 I have a Django app that has CustomUser. My model looks something like class CustomUser(AbstractBaseUser): def get_short_name(self): pass def get_full_name(self): pass firstName = models.CharField(max_length=300) middleName = models.CharField(max_length=300, blank=True) lastName = models.CharField(max_length=300, blank=True) username = models.CharField(unique=True, max_length=50) businessName = models.CharField(max_length=500, default=None) mobileNumber = models.CharField(max_length=20)

how to mention password field in serializer?

 ̄綄美尐妖づ 提交于 2020-05-26 09:13:46
问题 I have a custom user for authentication and want to create a serializer class for it my custom user's model is like this : class User (AbstractUser): bio = models.TextField(max_length=500, blank=True) birth_date = models.DateField(null=True, blank=True) image=models.FileField(null=True , blank=True) and my serializer is : class UserSerializer (serializers.ModelSerializer): class Meta: model = User fields = ('username' ,'email' ,'password' ,'firstname' , 'last name' ) how could I mention that

How to limit query results with Django Rest filters

戏子无情 提交于 2020-05-25 12:20:28
问题 I am working on an api built with Django Rest Framework . I have defined several model classes and I have also created some filters to apply on certain queries that happen in the specified api-endpoints . I am trying to apply LIMIT in the queryset but I would prefer to not use the Django notation e.x. Entry.objects.all()[:5] . Instead I would like to be able to apply the limit from inside the filter associated with the model. Until now I haven't find any solution. I am thinking that I should