Django: 'Data' object has no attribute 'save' - python

I am trying to save two int values into my postgreSQL database and am coming up show with the following traceback: 'Data' object has no attribute 'save'. I can't figure out what is wrong.
My Models...
from django.db import models
class Data(models.Model):
value = models.IntegerField()
curr_time = models.IntegerField()
My Views...
from django.shortcuts import render,redirect
from django.views.generic import View
from django.http import JsonResponse
from models import Data
class Data(View):
def get(self, request, date, val):
data = Data(value=int(val), curr_time=int(date))
data.save()
return JsonResponse({'status': 111})
I have looked up and down Stack Overflow and haven't found any answers. Anyone see anything glarringly wrong?

Your view class's name and model class's name are same. Change the view class's name to something else i.e: DataView

Yes, change the name of the view!
from models import Data
class Data(View): # oops, name clash
...
The class definition name stomped on the imported name Data.

Related

object has no attribute 'is_valid'

I am receiving an error, object has no attribute 'is_valid', when trying to insert form data into a form. Below is the structure of my code:
model.py
view.py
index.html
fm = Reg() doesn't really make sense. You can't assign a model/instance to a variable and consider it a form. Instead, create a form based on your model like this:
Create a file forms.py and create a ModelForm in it:
from django.forms import ModelForm
from mysite.models import Reg
class RegForm(ModelForm):
class Meta:
model = Reg
fields = ('<field_names>',)
ModelForm let's you create a form based on your Model.
Now import this in your views.py and use RegForm instead of Reg.

AttributeError: 'module' object has no attribute 'model' while model word is not written anywhere,

In djnago I have created Download model and it worked as expected but later when I tried to add new model 'Model' it just showing
AttributeError: 'Music' object has no attribute 'model'.
models.py looks like this:
from django.db import models
# Download/models.py.
class Download(models.Model):
name = models.CharField(max_length=50)
discription = models.CharField(max_length=50)
link = models.CharField(max_length=50)
imgages = models.ImageField(upload_to='media/')
def __str__(self):
return self.name
class Music(models.Model):
title = models.CharField(max_length=50)
def __str__(self):
return self.name
and here is an admin file
# Download/admin.py
from django.contrib import admin
from .models import Download,Music
# Register your models here.
admin.site.register(Download,Music)
If you pass two parameters to the register function, the first one is the model, and the second one is the ModelAdmin class for that model. Here you use it to register two models at once.
You can register a model without a ModelAdmin [Django-doc] by only specifying the model, but you thus can not specify multiple ones. You thus register the models with:
from django.contrib import admin
from .models import Download,Music
admin.site.register(Download)
admin.site.register(Music)
in Music you have self.name and I suppose you mean self.title

AttributeError: 'CSVupload' object has no attribute 'urls'

models.py
class AdminProduct(models.Model):
productname = models.CharField(max_length=50)
barcode = models.BigIntegerField()
def __str__(self):
return self.productname
class CSVupload(models.Model):
csv = models.FileField(upload_to='product/',null=True,blank=True)
admin.py
from django.contrib import admin
from .models import AdminProduct
from .models import CSVupload
# Register your models here.
admin.site.register(AdminProduct,CSVupload)
Want to register two models in admin,AdminProduct and CSVupload.
Getting an error :- AttributeError: 'CSVupload' object has no attribute 'urls'.
How do I do it?
The second parameter to register() is a ModelAdmin class. If you want to register multiple models at once, without specific ModelAdmins, you should pass them as an interable in the first parameter:
admin.site.register([AdminProduct, CSVupload])

AttributeError: (Class) object has no attribute '__name__' Creating ModelForms [Django & Python2.7]

This is my first time using Django and I am completely stuck at how to use ModelForms in my project. I have been able to follow the online tutorials this far but without ModelForms(to add data into a Postgresql database), I can't proceed onward. I am trying to simply make a form page that lets the users add a few inputs (2 datefields and 1 textfield) and by submitting that form, the data will be added to the database.
The error I have been getting is:
AttributeError: 'Hyuga_Requests' object has no attribute 'name' [where Hyuga_Request is a class set in the models.py]
models.py
from __future__ import unicode_literals
from django.db import models
from django.forms import ModelForm
class Hyuga_Requests(models.Model):
name = models.CharField(max_length=50)
s_date = models.DateField(auto_now=True)
e_date = models.DateField(auto_now=True)
reason = models.TextField(max_length=500)
def __unicode__(self):
return self.name
views.py
from django.shortcuts import render
from django import forms
from .forms import Hyuga_RequestForm
def create_req(request):
form = Hyuga_RequestForm()
context = {"form":form,}
return render(request,"request_form/requestform.html", context)
forms.py
from django import forms
from .models import Hyuga_Requests
from django.forms import ModelForm
class Hyuga_RequestForm(forms.ModelForm):
class Meta:
model = Hyuga_Requests()
fields = ['name','s_date','e_date','reason']
Please help this noobie...
Don't instantiate the model in the class Meta inside the Hyuga_RequestForm class.
model = Hyuga_Requests() should be model = Hyuga_Requests
model = Hyuga_Requests() -> model = Hyuga_Requests
The error come because you are calling the model on the form.
from django import forms
from .models import Hyuga_Requests
from django.forms import ModelForm
class Hyuga_RequestForm(forms.ModelForm):
class Meta:
model = Hyuga_Requests
fields = ['name','s_date','e_date','reason']
Note: i suggest to you use on the any class you define on python not use "_", you can check more about PEP8 and code styles here: https://www.python.org/dev/peps/pep-0008/

NameError: global name 'Model' is not defined

I have a simple Django app where I want to define two tables in the database: user and question.
I have the following models.py:
from django.db import models
class User(models.Model):
name = models.CharField(max_length=100)
#classmethod
def create(cls, name):
user = cls(name=name)
return user
class Question(models.Model):
content = models.CharField(max_length=300)
answer = models.CharField(max_length=200)
#classmethod
def create(cls, content, answer):
question = cls(content=content, answer=answer)
return user
In /questions, which is defined in views.py, I would like to display all objects of type question:
from django.views.generic.base import TemplateView
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.db import models
def questions(request):
questions = Model.objects.raw('SELECT * FROM questions')
return render_to_response('questions.html', questions)
However, I am getting:
NameError: global name 'Model' is not defined
Why is the Model object visible in models.py but not in views.py?
Also, is the way in which I query the database correct?
Answer to the question
Model is not visible in models.py - it's accessed as models.Model.
You're importing models but trying to use Model instead of models.Model.
Try this:
def questions(request):
questions = models.Model.objects.raw('SELECT * FROM questions')
return render_to_response('questions.html', questions)
Alternatively you can import Model directly:
from django.db.models import Model
Answer to the problem
I think this is a case of the XY problem.
What you really want to do is access all instances of the Question model. If you want to use function based views for some reason, you can import the Question model and use it directly with .all():
Function based views
question/views.py
from myapp.models import Question
def questions(request):
questions = Question.objects.all()
return render_to_response('questions.html', questions)
Class based views
The better option however, is to use a class based generic view.
An example from the documentation:
questions/views.py
from django.views.generic import ListView
from questions.models import Question
class QuestionList(ListView):
model = Question
urls.py
from django.conf.urls import url
from questions.views import QuestionList
urlpatterns = [
url(r'^questions/$', QuestionList.as_view()),
]
Class based views offer much bigger expressiveness than funciton based viwes. They also eliminate the need for a lot of code duplication since commonly changed parts of a view can be overridden individually (e.g. get_context_data)
You have to call your model in the following way inside your view
from myapp.models import Question
def questions(request):
questions = Question.objects.all()
return render_to_response('questions.html', questions)
for more information read the official documentation about views... https://docs.djangoproject.com/en/1.10/topics/http/views/

Categories