每次用户添加新帖子时,用户信息中的国家/地区名称都会重复。
例如。美国有人加贴,美国名字重复了很多次。
我尝试使用不同的 'country = Post.objects.all().distinct('country')' 然后我收到这个错误 'DISTINCT ON fields is not supported by this database backend'。
我的意见.py
def countries(request):
country = Post.objects.all().distinct('country')
context = {
'posts': country
}
return render(request, 'users/countries.html', context)
我的模型.py
from PIL import Image
from django.db import models
from django.urls import reverse
from django.utils import timezone
from django.db.models.signals import post_save
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
first_name = models.CharField(verbose_name="First name", max_length=255)
last_name = models.CharField(verbose_name="First name", max_length=255)
country = models.CharField(verbose_name="Country name", max_length=255)
city = models.CharField(verbose_name="City name", max_length=255)
email = models.EmailField(verbose_name="Email", max_length=255)
def __str__(self):
return self.username
class Post(models.Model):
title = models.CharField(max_length=255)
country = models.CharField(max_length=255)
city = models.CharField(max_length=255)
address = models.CharField(max_length=255)
email = models.EmailField(max_length=255)
phone = models.CharField(max_length=255)
website = models.URLField(max_length=255)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('users:blog')
一只斗牛犬
慕斯709654
相关分类