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

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

Related

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

ModelForm has no model class specified Error in Django

This is my forms.py file!!!
from django import forms
from . models import URLModel
class URLForm(forms.ModelForm):
class Meta:
model = 'URLModel'
fields = ['url']
This is my models.py file
from django.db import models
# Create your models here.
class URLModel(models.Model):
url=models.URLField(unique=True)
short=models.CharField(max_length=200,unique=True)
def __str__(self):
return self.url
Now i just wanted to ask that why this error is coming and i have revieved and i cant observe any error for real...please help
# Remove single quote from your model specification in forms.py
from django import forms
from .models import URLModel
class URLForm(forms.ModelForm):
class Meta:
model = URLModel
fields = ('url',)

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/

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

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.

Admin Form Custom Multiple select box

I have completed my models and I am trying to customise the admin model for a particular model.
Here is my admin.py
from project.base.models import *
from django.contrib import admin
from django import forms
class StaffModuleForm(forms.ModelForm):
user = forms.ModelChoiceField(queryset=User.objects.all())
class Meta:
model = StaffModule
class StaffModuleAdmin(admin.ModelAdmin):
form = StaffModuleForm
class Meta:
pass
Modules = [UserProfile, Student, Module, StaffModule, Exercise, Result]
admin.site.register(Modules)
and here is the model in question
class StaffModule(models.Model):
user = models.ForeignKey(User)
modules = models.ManyToManyField(Module) # Field name made lowercase.
department = models.CharField(max_length=6)
def __unicode__(self):
return str(self.user)
Here is the error I am currently unable to pass:
AttributeError at /admin/base/staffmodule/2/
type object 'StaffModuleAdmin' has no attribute '_meta'
Here is the trace: http://pastebin.com/tCJ9Q978
I would really appreciate some help, as I believe I am following the django admin docs and i am not quite sure what the issue is.
Thanks.
The problem is in this:
class StaffModuleAdmin(admin.ModelAdmin):
form = StaffModuleForm
class Meta:
pass
Inner Class Meta should not be defined for the admin.ModelAdmin class.

Categories