[Fixed]-Django 'function' object has no attribute 'objects'

27๐Ÿ‘

โœ…

your view function name is defined as Like and your model is named Like

you define Like as a function so when you go to access Like.objects python does not see your model Like but the function Like

you could rename your view function

url(r'^like/(?P\d+)/$', 'pet.views.change_name_no_conflict', name = 'Like' )


def change_name_no_conflict(request,picture_id):
  pass
๐Ÿ‘คdm03514

7๐Ÿ‘

Model name and view name shouldnโ€™t be same.

๐Ÿ‘คSandeep G

1๐Ÿ‘

This is because your function name in view.py and model name in models.py are the same.
You can change the function name or model name.

Another solution is:

from .models import modelname as modelname2

def modelname(request):
    obj_list_ads = modelname2.objects.all()

in this code function name is modelname.

๐Ÿ‘คDarwin

0๐Ÿ‘

I got this same error when doing

 from django.contrib.auth import get_user_model
 User = get_user_model

adding () solved the error:

 from django.contrib.auth import get_user_model
 User = get_user_model()
๐Ÿ‘คDevB2F

0๐Ÿ‘

function name and model name does depend on name, function name should be same as url name we define url in urls.py model name depend on function data member, its means as for example when we take data from user and save in database then we call that object from its model name ex= u_data = registration() ,this is used for user data seve and define that fun where we send data to save means target url and in its related view.

๐Ÿ‘คAnkit Anand

0๐Ÿ‘

I hit this error because my viewโ€™s query set looked like this:

class IngredientList(generics.ListAPIView):
  queryset = Ingredient.objects.all # This line is wrong
  serializer_class = IngredientSerializer

I fixed it by adding a set of parenthesis to the queryset line:

class IngredientList(generics.ListAPIView):
      queryset = Ingredient.objects.all() # Fixed
      serializer_class = IngredientSerializer

-1๐Ÿ‘

Define object:

objects = models.Manager()

Your error will solve!

๐Ÿ‘คAman Bothra

Leave a comment