Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to resole: type object 'Member' has no attribute '_default_manager, django 1.10

I have the following problem: When I am trying to delete a member from my DB via the template, I'm receiving the following error:

type object 'Member' has no attribute '_default_manager

I'm not sure what am I doing wrong, because everything seems to be as normal as possible. Bellow are my model, view and template snippet for my issue.

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']

And my Delete View:

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')

and my form:

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 ."),
            }
    }

and my Delete url:

url(r'^delete-member/(?P<pk>\d+)/delete/$', DeleteMember.as_view(),
        name='delete_member'),

and in the template:

<a class="btn btn-danger btn-edit" role="button" href="{% url "website:delete_member" pk=member.id %}">Delete</a>

And my 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'

I will appreciate any help.

like image 701
pussycat Avatar asked Jan 28 '17 12:01

pussycat


2 Answers

I have found the issue, for some reason as I have called my model Member and my view Member as well, it was conflicted. So I just renamed the view.

like image 150
pussycat Avatar answered Oct 18 '22 07:10

pussycat


This error occurs when django points to a different object apart from your model, so just check in serializers or signals or anything that you have imported in your views. You will find another object that has the same name as your model

like image 44
David Rugeihyamu Avatar answered Oct 18 '22 06:10

David Rugeihyamu