我有以下问题:当我尝试通过模板从数据库中删除成员时,我收到以下错误:
类型对象'Member'没有属性'_default_manager
我不知道我做错了什么,因为一切似乎都很正常。Bellow是我的模型,视图和模板片段为我的问题。
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
class Member(models.Model):
"""Model for a member"""
surname = models.CharField(max_length=255)
names = models.CharField(max_length=255)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
dob = models.DateField()
# anniversary = models.ForeignKey(Anniversary)
email = models.EmailField()
phone = PhoneNumberField(help_text='Please, use this format +3232')
address = models.CharField(max_length=255)
suburb = models.CharField(max_length=255, choices=Suburbs_list, default=Suburbs_list[0][0])
province = models.CharField(max_length=255, choices=Provinces_List, default=Provinces_List[8][1])
country = models.CharField(max_length=255, default='Unite Kingdom')
previous_church = models.CharField(max_length=255)
added_date = models.DateField(default=datetime.date.today)
def __str__(self):
return self.surname
class Meta:
ordering = ['-suburb', 'province']
字符串
我的删除视图:
class DeleteMember(LoginRequiredMixin, generic.DeleteView):
"""
This view will be responsible of deleting an member
"""
model = Member
template_name = 'members/delete_member.html'
success_url = reverse_lazy('website:home')
型
我的形式:
class MemberForm(forms.ModelForm):
dob = forms.DateField(widget=extras.SelectDateWidget, label='Date of Birth')
phone = forms.CharField(label='Phone Number',
widget=forms.TextInput(attrs={'placeholder': '+41524242424'}))
class Meta:
model = Member
exclude = ['added_date']
error_messages = {
'phone': {
'max_length': _("Please use this format +41524242424 ."),
}
}
型
删除URL:
url(r'^delete-member/(?P<pk>\d+)/delete/$', DeleteMember.as_view(),
name='delete_member'),
型
在模板中:
<a class="btn btn-danger btn-edit" role="button" href="{% url "website:delete_member" pk=member.id %}">Delete</a>
型
我的traceback:
Internal Server Error: /delete-member/1/delete/
Traceback (most recent call last):
File "/project/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/project/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/project/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/project/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/project/lib/python3.5/site-packages/django/contrib/auth/mixins.py", line 56, in dispatch
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/project/lib/python3.5/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/project/lib/python3.5/site-packages/django/views/generic/detail.py", line 115, in get
self.object = self.get_object()
File "/project/lib/python3.5/site-packages/django/views/generic/detail.py", line 32, in get_object
queryset = self.get_queryset()
File "/project/lib/python3.5/site-packages/django/views/generic/detail.py", line 68, in get_queryset
return self.model._default_manager.all()
AttributeError: type object 'Member' has no attribute '_default_manager'
型
我将感激任何帮助。
3条答案
按热度按时间wgxvkvu91#
我发现这个问题,出于某种原因,我称之为我的模范成员和我的观点成员,这是矛盾的。所以我只是重命名了视图。
r8xiu3jd2#
当django指向一个与你的模型不同的对象时,这个错误就会发生,所以只要签入序列化器或信号或任何你在视图中导入的东西。您将找到另一个与您的模型同名的对象
dpiehjr43#
在我的例子中,错误是在视图中,由于之前的复制和粘贴,我为变量模型传递了一个Form。
字符串
我希望对任何遇到这个错误的人有所帮助。