Я получаю ошибку 'ReverseManyToOneDescriptor' объект не имеет атрибута 'exclude'.
Пытаюсь получить запрос типа "один ко многим" с фильтрами, но продолжаю получать эту ошибку, прошу помочь
def user_profile(request,pk):
profileObj = Profile.objects.get(id = pk)
topSkill = Profile.skill_set.exclude(description__isnull=True)
otherSkill = Profile.skill_set(description = "")
context = {"profile":profileObj,'topSkills':topSkills,"otherSkills":otherSkills}
return render(request, 'users/user_profile.html', context)
но я продолжаю получать эту ошибку, объект 'kindly assistReverseManyToOneDescriptor' не имеет атрибута 'exclude'
Вот мои модели
class Profile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, null = True, blank = True)
name = models.CharField(max_length= 500, null = True, blank = True)
email = models.EmailField(max_length= 500, null = True, blank = True)
username = models.CharField(max_length= 500, null = True, blank = True)
location = models.CharField(max_length= 500, null = True, blank = True)
short_intro = models.CharField(max_length= 300, null = True, blank = True)
bio = models.TextField(max_length= 500, null = True, blank = True)
profile_image = models.ImageField(upload_to = "profiles/", default = "profiles/user-default.png", null = True, blank = True)
social_github = models.CharField(max_length= 500, null = True, blank = True)
social_twitter = models.CharField(max_length= 500, null = True, blank = True)
social_linkedIn = models.CharField(max_length= 500, null = True, blank = True)
social_youtube = models.CharField(max_length= 500, null = True, blank = True)
social_website = models.CharField(max_length= 500, null = True, blank = True)
created = models.DateTimeField(auto_now_add = True)
id = models.UUIDField(default = uuid.uuid4, unique = True, primary_key = True, editable = False)
def __str__(self): #Django will call __unicode__ when it needs to render an object in a context where a string representation is needed
return str(self.user.username)
class Skill(models.Model):
owner = models.ForeignKey(Profile,null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length = 200,null=True,blank=True)
description = models.TextField(max_length=500, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, unique=True,primary_key=True,editable=False)
def __str__(self):
return str(self.name)
У меня есть следующие предложения, включая одно, уже высказанное @AbdulAzizBarkat в комментарии выше:
Лучше использовать
get_object_or_404()
.Прямое исключение элементов из модели вместо использования обратных отношений.
Таким образом, вид должен быть следующим:
from django.shortcuts import get_object_or_404
def user_profile(request,pk):
profileObj = get_object_or_404(Profile, id=pk)
topSkill = Skill.objects.exclude(description__isnull=True)
otherSkill = Skill.objects.exclude(description = "")
context = {"profile":profileObj,'topSkills':topSkills,"otherSkills":otherSkills}
return render(request, 'users/user_profile.html', context)
Однако я не понял логику Skill.objects.exclude(description="")
, поскольку topSkill
уже исключает те элементы, которые имеют null=True.