I've have something like this in django: "module 'app.forms' has no attribute 'JobsFormSet'"
views.py:
http://pastebin.com/Kwup5s1x
forms.py:
http://pastebin.com/WyMuW3vn
I don't know where is a problem. I'm beginner in django. I'm pretty sure this code is correct, because I'm using from this tutorial (in polish)
Th error you get is
module 'app.forms' has no attribute 'JobsFormSet'
which probably comes from the lines
form = self.get_form()
jobs = forms.JobsFormSet(self.request.POST)
since you try to get JobsFormSet attribute in "forms" instead of "form".
Try this:
jobs = form.JobsFormSet(self.request.POST)
Related
I'm trying to create a JSON response in a django project; but something goes wrong!
applications_list = []
for project in projects_list:
application_information = {
``` some keys and values ```
}
applications_list.append(application_information)
context = {'applications': applications_list}
return render(request, self.template_name, context)
But I got this error:
'dict' object has no attribute 'META'
Should be mentioned that keys and values of application_information are serialized. I also tried json.dumps() but didn't work.
I wonder what have been missed?
Sounds like you accidentally modified the request variable before you passed it to render call.
Check what happens with that variable in the code before the snippet you posted, or expand your question with additional code lines.
So i've got django-postman installed in my project. I've got the app installed and the urls are added but it's still not working.
When I try and send a message to the user test for example it's saying "Some usernames are unknown or no more active: test." which makes me think it's trying to use the wrong user model because the username exists in the database, it just can't find it.
I've got these in my settings if it helps.
POSTMAN_DISALLOW_ANONYMOUS = False
POSTMAN_DISALLOW_MULTIRECIPIENTS = False
AUTH_USER_MODEL = 'accounts.User'
Looking at the code for django-postman I have found out the issue. In postman.future_1_5.py it just imports User instead of what I need.
How can I change this code? Is there a way I can keep a file within my application and use that instead?
I'm thinking this will fix my issue:
from __future__ import unicode_literals
from accounts.models import MyUser
MyUser.USERNAME_FIELD = 'username'
MyUser.get_username = lambda self: self.username
def get_user_model():
return MyUser
Modifying future_1_5.py is not the solution to your issue, and is a very bad idea anyway. As its name implies, this file is intended to make the code runnable with Django before version 1.5.
I suppose you're running at least Django 1.5, so if this fallback is fired it means that your custom user model could not be imported.
The right explanation is that your setting is wrong. It has to name the effective model:
AUTH_USER_MODEL = 'accounts.MyUser'
i have following model:
class comment(models.Model):
userid=models.CharField(max_length=140)
apparelid=models.IntegerField(blank=True)
desc=models.TextField(blank=True)
def __unicode__(self):
return self.userid
form :
class commentForm(ModelForm):
class Meta:
model=comment
exclude=('userid','apparelid',)
and view as follows:
def comment(request,title_id):
if request.method=='POST':
form=commentForm(request.POST)
if form.is_valid():
new=form.save(commit=False)
new.userid=request.user.username
new.apparelid=title_id
new.save()
return HttpResponseRedirect('/')
else:
form=commentForm()
template=loader.get_template('apple3/comment.html')
context=RequestContext(request,{
'form':form,
}
)
return HttpResponse(template.render(context))
whenever i open my page containg above form it show an error as follows:
Exception Type: AttributeError
Exception Value: 'function' object has no attribute 'objects'
You probably import comment model from inside your view and then use comment again as view name. That's why the error gets thrown.
Please use different name for your view and model if you use them in the same module.
Problem at hand seems to be solved by #mariodev. Additionaly, I'd recommend two following steps as a mean of avoiding similar problems in future:
Read PEP8, the Style Guide for Python Code thoroughly
Use only packages and modules import.
Following those two links will make your code more pythonic and less error-prone.
The name of the model class and the view function is same which is resulting in the error:
Exception Value: 'function' object has no attribute 'objects'
You may use a different naming conventions for your classes and functions. As per PEP8, first letter of the class name should be capital/uppercase and the function's name should be lowercase.
So in your case, if you have to keep the names exactly same, you may rename your Model Class to Comment and let your view function's name be comment and that should solve the problem.
I am trying to get a value from request and really suprised an error is being raised.
def product_view(request):
lookup_type_user = request.GET.get('lookup_type', '')
LOOKUP_TYPE_CHOICES = (
('gt', '>'),
('lt', '<'),
)
class ProductFilter(django_filters.FilterSet):
lookup_type = django_filters.ChoiceFilter(choices=LOOKUP_TYPE_CHOICES)
price = django_filters.NumberFilter(lookup_type=lookup_type_user)
This line, being basically the same, works fine.
price = django_filters.NumberFilter(lookup_type='gte')
I am not posting the error message because it's a package relevant one and since the line above where I provided lookup_type by hand did not raise anything I am assumming it has nothing to do with that package but the code above.
Can you see what goes wrong here?
#EDIT
Is there a way I can print out the request to see what it contains exactly?
I got it working. It was my ignorance. I had to redefine lookup_type in forms.py instead. Like this:
lookup_type = forms.ChoiceField(choices=LOOKUP_TYPE_CHOICES)
and not:
lookup_type = django_filters.ChoiceFilter(choices=LOOKUP_TYPE_CHOICES)
Because what django-filter was doing, it was trying to filter the lookup_type field, which does not exist in my models. It was throwing an error FieldError at/. Cannot resolve keyword 'lookup_type' into field, which I did not know about because another app I am using - django_tables2 modified this error to something else, which tricked me successfully.
It's probably a useless thread now but I just want to let pleople who tried to solve this, know.
I am trying to use django-datatrans to translate a MarkupField (from django-markitup) on a model. Both apps work correctly independently, but when I register datatrans to translate a MarkupField then I can't add objects in the admin anymore.
Relevant code:
from django.db import models
from markitup.fields import MarkupField
from datatrans.utils import register
class Work(models.Model):
title = models.CharField(max_length=500)
content = MarkupField(help_text=MARKDOWN_HELP)
class WorkTranslation(object):
fields = ('title', 'content')
register(Work, WorkTranslation)
When I try to add a new Work-object in the admin I get the following error:
'unicode' object has no attribute 'raw'
The error happens here, in the markitup-module (in the line rendered = render_func(value.raw):
.../lib/python2.7/site-packages/markitup/fields.py in pre_save
def pre_save(self, model_instance, add):
value = super(MarkupField, self).pre_save(model_instance, add)
rendered = render_func(value.raw)
setattr(model_instance, _rendered_field_name(self.attname), rendered)
return value.raw
Local vars when failing:
add: False
model_instance: <Work: This is the title>
value: u'This is the content.'
self: <markitup.fields.MarkupField: content>
I have tried to inspect the variable value when the Work class is not registered for translation. In that case (and then it does work correctly) it is not a unicode string but an instance of markitup.fields.Markup.
I have not been able to figure out why the type changes and I realize this question is pretty specific. But I hope someone has insights anyway..
Had the same issue with django-modeltranslation and django-markitup when testing it:
class ModelTests(TestCase):
def test_my_class(self):
self.assertRaises(IntegrityError, models.MyClass.objects.create)
It works for me with:
class ModelTests(TestCase):
def test_my_class(self):
with self.assertRaises(IntegrityError):
models.MyClass.objects.create(info='', info_de='')
Where my installed languages are en and de. My default language is en. info is my field with markitup and translation. (I'm testing here that a field is required on MyClass, therefore the IntegrityError.)
(Btw. this produces a slightly different error:
class ModelTests(TestCase):
def test_my_class(self):
self.assertRaises(IntegrityError, models.MyClass.objects.create(info=''))
Error:
AttributeError: 'NoneType' object has no attribute 'raw'
)
Maybe this helps someone.