I want to set a username for each object. Here is my models.py
from django.contrib.auth.models import User from django.conf import settings # Tweet Model class TweetModel(models.Model): text = models.TextField(max_length=300, blank=False) created_at = models.DateTimeField(auto_now_add=True) owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True) username = models.CharField(max_length=100, blank=True)
And viewset.py
from .models import TweetModel from rest_framework import viewsets, permissions # TweetViewset class TweetViewset(viewsets.ModelViewSet): permission_classes = [ permissions.IsAuthenticated ] serializer_class = TweetSerializer def get_queryset(self): ordered = TweetModel.objects.order_by('-created_at') return ordered def perform_create(self, serializer): serializer.save(owner=self.request.user, username=self.request.user.username)
But it is not working. Error is
django.db.utils.ProgrammingError: column tweets_tweetmodel.username does not exist LINE 1: ...el"."created_at", "tweets_tweetmodel"."owner_id", "tweets_tw...
Can you help, please?
https://stackoverflow.com/questions/65821040/how-to-return-username-instead-of-user-object-in-drf January 21, 2021 at 12:05PM
没有评论:
发表评论