AttributeError:类型对象'Categories'在Django中没有属性'user_id__isnull'
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AttributeError:类型对象'Categories'在Django中没有属性'user_id__isnull'相关的知识,希望对你有一定的参考价值。
当我在Django中使用查询集时,出现此错误:
AttributeError:类型对象'Categories'没有属性'user_id__isnull'
The Categories模型在User模型上具有外键名为用户。
当我使用查询集:_Categories.objects.filter(Q(Categories.user_id__isnull==True ) | Q(user=user))_
时,我本质上想做这样的事情:
SELECT * FROM Categories WHERE user_id=NULL | user.id=user.id
((user.id = user.id代表当前登录的用户)
forms.py:
from .models import Task, Categories
from django import forms
from django.forms import ModelForm
from django.db.models import Q
class TaskForm(ModelForm):
task_title = forms.CharField(max_length=100)
task_description = forms.CharField(widget=forms.Textarea)
due_date = forms.DateTimeField()
is_completed = forms.BooleanField()
#categories = forms.ModelChoiceField(empty_label="---None---")
class Meta:
model = Task
fields = ['task_title', 'task_description', 'due_date', 'is_completed', 'categories', 'parent']
def __init__(self, user, *args, **kwargs):
# Get all the categories from the database for that specifc user
super(TaskForm, self).__init__(*args, **kwargs)
self.fields['categories'].queryset = Categories.objects.filter(Q(Categories.user_id__isnull==True ) | Q(user=user))
models.py:
from django.db import models
from django.db.models import Q
from users.models import CustomUser
from django.urls import reverse
from django.contrib.auth import get_user_model
class Categories(models.Model):
category_type = models.CharField(max_length=50)
user = models.ForeignKey(CustomUser, null = True, on_delete=models.CASCADE)
def __str__(self):
return '%s ' % (self.category_type)
def get_absolute_url(self):
return reverse('task_list')
class Task(models.Model):
task_title = models.CharField(max_length=100)
task_description = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
due_date = models.DateTimeField()
is_completed = models.BooleanField(default=False)
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
categories = models.ForeignKey('Categories', on_delete=models.CASCADE)
parent = models.ForeignKey("self", on_delete=models.CASCADE)
class Meta:
verbose_name = "Task"
verbose_name_plural = "Tasks"
def __str__(self):
return '%s ID: %s' % (self.task_title, self.last_name)
def get_absolute_url(self):
return reverse('task_detail')
CustomUser模型:
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
nationality = models.CharField(null=True, blank=True, max_length=60)
您的查询集语法错误。应该是:
Categories.objects.filter(Q(user_id__isnull=True) | Q(user=user))
以上是关于AttributeError:类型对象'Categories'在Django中没有属性'user_id__isnull'的主要内容,如果未能解决你的问题,请参考以下文章
AttributeError:'numpy.ndarray'对象没有属性'units'
django - AttributeError:类型对象'file'没有属性'set_user'
AttributeError:类型对象“numpy.ndarray”没有属性“__array_function__”
Python:AttributeError:'ResultSet'对象没有属性'get'
AttributeError:'str'对象没有属性'author'
解决编码问题:AttributeError: 'str' object has no attribute 'decode'