前提・実現したいこと
現在Djangoにて投稿・コメント機能を実装しているのですが、migrateを行えずこのようなエラーが発生してしまいました。
発生している問題・エラーメッセージ
ValueError: Field 'id' expected a number but got '退会済みのユーザー'.
該当のソースコード
accounts/models.py
1 2 3 4class UserManager(BaseUserManager): 5 6 def _create_user(self, email, password, is_staff, is_superuser, **extra_fields): 7 if not email: 8 raise ValueError('Users must have an email address') 9 now = timezone.now() 10 email = self.normalize_email(email) 11 user = self.model( 12 email=email, 13 is_staff=is_staff, 14 is_active=True, 15 is_superuser=is_superuser, 16 last_login=now, 17 date_joined=now, 18 **extra_fields 19 ) 20 user.set_password(password) 21 user.save(using=self._db) 22 return user 23 24 def create_user(self, email, password, **extra_fields): 25 return self._create_user(email, password, False, False, **extra_fields) 26 27 def create_superuser(self, email, password, **extra_fields): 28 user = self._create_user(email, password, True, True, **extra_fields) 29 user.save(using=self._db) 30 return user 31 32class User(AbstractBaseUser, PermissionsMixin): 33 name = models.CharField(max_length=50, null=True, blank=True) 34 email = models.EmailField(max_length=254,unique=True) 35 username= models.CharField(max_length=30,null=False, blank=True) 36 user_id = models.CharField(max_length=30,null=False, blank=True) 37 profile_image=models.ImageField(upload_to='media',null=True) 38 self_introduction = models.TextField(max_length=300, null=False, blank=True) 39 is_staff = models.BooleanField(default=False) 40 is_superuser = models.BooleanField(default=False) 41 is_active = models.BooleanField(default=True) 42 last_login = models.DateTimeField(null=True,blank=True) 43 date_joined = models.DateTimeField(auto_now_add=True) 44 45 46 USERNAME_FIELD = 'email' 47 EMAIL_FIELD = 'email' 48 REQUIRED_FIELDS = [] 49 50 objects = UserManager() 51 52 def get_absolute_url(self): 53 return "/users/%i/" % self.pk 54 55 def __str__(self): 56 return self.username 57
post/models.py
1 2 3from django.db import models 4from accounts.models import User 5from django.utils import timezone 6 7 8class Post(models.Model):#投稿モデル 9 username=models.ForeignKey(User,on_delete=models.SET_DEFAULT,default='退会済みのユーザー') 10 title=models.CharField(max_length=100,blank=False,null=False) 11 post_img1=models.ImageField(upload_to='post1',blank=True,null=True) 12 post_img2=models.ImageField(upload_to='post2',blank=True,null=True) 13 post_img3=models.ImageField(upload_to='post3',blank=True,null=True) 14 text=models.TextField(max_length=5000,blank=False,null=False) 15 add_time=models.DateTimeField(default=timezone.now) 16 tag = models.TextField(max_length=200,blank=True,null=True) 17 18 def __str__(self): 19 return self.title 20 21 22class Comment(models.Model):#コメントモデル 23 target_post=models.ForeignKey(Post,on_delete=models.CASCADE) 24 username=models.ForeignKey(User,on_delete=models.SET_DEFAULT,default='退会済みのユーザー') 25 text=models.TextField(max_length=500) 26 add_time=models.DateTimeField(default=timezone.now) 27 28 def __str__(self): 29 return self.text 30 31class Reply(models.Model):#返信モデル 32 target_comment=models.ForeignKey('Comment',on_delete=models.CASCADE) 33 username=models.ForeignKey(User,on_delete=models.SET_DEFAULT,default='退会済みのユーザー') 34 text=models.TextField(max_length=500) 35 time=models.DateTimeField(default=timezone.now) 36 37 def __str__(self): 38 return self.text 39 40
post/forms.py
1 2from django.forms import ModelForm 3from post.models import Post, Comment,Reply 4 5 6class PostCreateForm(ModelForm): 7 class Meta: 8 model=Post 9 fields=[ 10 'title','text','post_img1','post_img2','post_img3','tag', 11 ] 12 13class CommentForm(ModelForm): 14 class Meta: 15 model=Comment 16 fields=[ 17 'text' 18 ] 19 20class ReplyForm(ModelForm): 21 class Meta: 22 model=Reply 23 fields=[ 24 'text' 25 ] 26 27
post/views.py
1 2from django.contrib.auth.mixins import LoginRequiredMixin 3from django.shortcuts import redirect, get_object_or_404 4from post.forms import PostCreateForm,CommentForm,ReplyForm 5from django.urls import reverse_lazy 6from post.models import Post,Comment,Reply 7from django.views.generic import ( 8 CreateView, 9 DetailView, 10 11) 12 13 14 15class PostCreateView(CreateView,LoginRequiredMixin):#投稿機能 16 template_name = 'post/post_create.html' 17 form_class = PostCreateForm 18 success_url = reverse_lazy('home') 19 20 def form_valid(self, form): 21 form.instance.username=self.request.user 22 return super().form_valid(form) 23 24 25class CommentView(CreateView,LoginRequiredMixin):#コメント機能 26 template_name = 'post/comment.html' 27 model = Comment 28 form_class = CommentForm 29 30 31 def form_valid(self, form ): 32 form.instance.username =self.request.user 33 post_pk = self.kwargs['pk'] 34 post = get_object_or_404(Post, pk=post_pk) 35 comment = form.save(commit=False) 36 comment.target_post= post 37 comment.save() 38 return redirect('detail',pk=post_pk) 39 40 41 def get_context_data(self, **kwargs): 42 context = super().get_context_data(**kwargs) 43 context['post'] = get_object_or_404(Post, pk=self.kwargs['pk']) 44 return context 45 46 47class ReplyCreateView(CreateView,LoginRequiredMixin):#返信機能 48 template_name = 'post/reply.html' 49 model = Reply 50 form_class = ReplyForm 51 52 def form_valid(self, form): 53 form.instance.username = self.request.user 54 comment_pk = self.kwargs['pk'] 55 comment = get_object_or_404(Comment, pk=comment_pk) 56 reply = form.save(commit=False) 57 reply.target_comment = comment 58 reply.save() 59 return redirect('detail', pk=comment.target_post.pk) 60 61 def get_context_data(self, **kwargs): 62 context = super().get_context_data(**kwargs) 63 comment_pk = self.kwargs['pk'] 64 comment = get_object_or_404(Comment, pk=comment_pk) 65 context['post'] = comment.target_post 66 return context 67 68 69 70class PostDetailView(DetailView):#投稿の詳細 71 template_name = 'post/post_detail.html' 72 model = Post 73 74 75
試したこと
一度この書き方でサイトをリリースしているのですが今回は何故かエラーが発生してしまいました。idの取得と記載されていたのでpkをidに変更するなどしましたが同じ動作しませんでした。
補足情報(FW/ツールのバージョンなど)
前回よりの変更点としてsqlite3→postgressqlに変更
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。