Home > front end >  AttributeError: module 'django.db.models' has no attribute 'Aqtau'
AttributeError: module 'django.db.models' has no attribute 'Aqtau'

Time:07-17

I work with django first time. And fced with this error. Can You help me to solve. Erro like this, I have many models and data in there, and I put this data to html like table, after I want to change values in data, but in views I cant search my models

My models py like this: (in city_name I put name of Model there it is Aqtau )

from django.db import models
class Aqtau(models.Model):
  city_name = models.CharField(max_length=255)
  KT_by_projects_10g = models.CharField(max_length=255)
  KT_by_projects_100g = models.CharField(max_length=255)
  KT_on_facts_10g = models.CharField(max_length=255)
  KT_on_facts_100g = models.CharField(max_length=255)

My views to put data to html:

def index(request):
  Aqtau_city = Aqtau.objects.all().values()
  template = loader.get_template('index.html')
  context = {
    'Aqtau_city': Aqtau_city,
  }
  return HttpResponse(template.render(context, request))

HTML looks like:

<tr>
  {% for x in Aqtau_city %}
  <td>Aqtau</td>
  <td>{{ x.KT_by_projects_10g }} </td>
  <td>{{ x.KT_by_projects_100g }} </td>
  <td>{{ x.KT_on_facts_10g }} </td>
  <td>{{ x.KT_on_facts_100g }} </td>

  <td><a href="update/{{ x.city_name }}/{{ x.id }}">update</a></td>

  {% endfor %}
</tr>

urls.py looks like:

urlpatterns = [
    path('', views.index, name='index'),
    path('update/<str:city_name>/<int:id>', views.update, name='update'),

And views update like: There i want to get model by getattr, but django db.models don't search Aqtau class.

def update(request, city_name, id):
  model = getattr(models, city_name)
  mymember = model.objects.get(id=id)
  template = loader.get_template('update.html')
  context = {
    'mymember': mymember,
  }
  return HttpResponse(template.render(context, request))

Error like this
model = getattr(models, city_name) AttributeError: module 'django.db.models' has no attribute 'Aqtau'

CodePudding user response:

You need to import your model into your view.py file instead of using getattr(models, city_name)

something like this:

from apps.city_app.models import Aqtau

After that, you will be able to get objects of that model, something like the below:

mymember = Aqtau.objects.get(id=id)

Also I suggest to use 'get_object_or_404' for you fetching,Because it will prevent form DoesNotExist Error:

mymember = get_object_or_404(Aqtau, id=id)

If you wanted to use 'get_object_or_404' you can import it like below:

from django.shortcuts import get_object_or_404

CodePudding user response:

I don't know why you are using this method getattr personally, I'm using the get_object_or_404 function in the update method it's will throw a 404 exception if the model not found

from django.shortcuts import get_object_or_404

def update(request, city_name, id):
    obj = get_object_or_404(Aqtau, id=id)
    context = {
        "object": obj
    }
    return render(request, "update.html", context)  

Note you should include the model form class in the update method to draw the form of model attributes you can see full documentation in this link

  • Related