object has no attribute

370 views
Skip to first unread message

Pacem

unread,
Jan 27, 2011, 4:41:36 AM1/27/11
to Django users
Hi. Im trying to create a web-site with a book from pact.com (Django
website development) as a basis. It has gone pretty well so far, but i
have encountered a problem when changing a form in models.py from
ForeingKey to OneToManyKey.
I think i know where the problem is, but i cant find a resolution.

view.py:
---------------------------------------------------------------------------------------
from django.http import Http404, HttpResponseRedirect
from django.template import RequestContext
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response

def startside(request):
return render_to_response(
'startside.html',
RequestContext(request)
)

@login_required
def brukerside(request, username):
try:
bruker = User.objects.get(username=username)
except User.DoesNotExist:
raise Http404(u'Bruker eksisterer ikke.')

snarveier = bruker.brukersnarvei_set.all()

variabler = RequestContext(request, {
'brukernavn': username,
'snarveier': snarveier,
})
return render_to_response('brukerside.html', variabler)

def utlogg(request):
logout(request)
return HttpResponseRedirect('/')

---------------------------------------------------------------------------------

models.py:
---------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User

class Program(models.Model):
""" Inneholder link til forskjellige programmer """
tittel = models.CharField(max_length=64, unique=True)
link = models.URLField(unique=True)

def __unicode__(self):
return self.tittel

class Meta:
verbose_name_plural="Programmer"

class Brukersnarvei(models.Model):
""" Kobler sammen brukere og programmer """
brukere = models.OneToOneField(User)
programmer = models.ManyToManyField(Program)

def __unicode__(self):
return u'%s' % (self.brukere)

class Meta:
verbose_name_plural="Brukersnarveier"

-------------------------------------------------------------------------------

Error message:
'User' object has no attribute 'brukersnarvei_set'

-------------------------------------------------------------------------------

I have chosen to put norwegian names where i can to better see the
difference between python/django-set standards and what i have more
controll over.

I understand the problem lies in the view; "snarveier =
bruker.brukersnarvei_set.all()".
I dont completely understand how the _set-function works, and i think
this is not usable on a OneToManyField?

Any pointers on where to find information on this, or a direct
resolution (or alternatives) are highly appreciated!

Tom Evans

unread,
Jan 27, 2011, 7:21:57 AM1/27/11
to django...@googlegroups.com
On Thu, Jan 27, 2011 at 9:41 AM, Pacem <meta...@gmail.com> wrote:
> Hi. Im trying to create a web-site with a book from pact.com (Django
> website development) as a basis. It has gone pretty well so far, but i
> have encountered a problem when changing a form in models.py from
> ForeingKey to OneToManyKey.

You changed it to a OneToOneKey (OneToMany is another way of saying
ForeignKey). OneToOneKey relationships are modelled as a single
attribute, not a set, so it would simply be

snarveier = bruker.brukersnarvei

Using "python manage.py shell" you can explore what attributes your
objects have. Create a user and run dir(user_instance).


Cheers

Tom

Pacem

unread,
Jan 27, 2011, 9:01:39 AM1/27/11
to Django users
> You changed it to a OneToOneKey (OneToMany is another way of saying
> ForeignKey). OneToOneKey relationships are modelled as a single
> attribute, not a set, so it would simply be
>
> snarveier = bruker.brukersnarvei
>
> Using "python manage.py shell" you can explore what attributes your
> objects have. Create a user and run dir(user_instance).
>
> Cheers
>
> Tom

ahh. i thought _set was meant as a werb. now it makes a lot more sence
to me.
but alas, it still wont work.

Here is what i found through the terminal.

---------------------------------------------------------------
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> User.objects.all()
[<User: itss>]
>>> user = User.objects.get(id=1)
>>> dir(user)
['DoesNotExist', 'MultipleObjectsReturned', '__class__',
'__delattr__', '__dict_
_', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__',
'__init__
', '__metaclass__', '__module__', '__ne__', '__new__', '__reduce__',
'__reduce_e
x__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_
_unicode__', '__weakref__', '_base_manager', '_collect_sub_objects',
'_default_m
anager', '_deferred', '_get_FIELD_display', '_get_message_set',
'_get_next_or_pr
evious_by_FIELD', '_get_next_or_previous_in_order', '_get_pk_val',
'_get_unique_
checks', '_message_set', '_meta', '_perform_date_checks',
'_perform_unique_check
s', '_set_pk_val', '_state', 'brukersnarvei', 'check_password',
'clean', 'clean_
fields', 'date_error_message', 'date_joined', 'delete', 'email',
'email_user', '
first_name', 'full_clean', 'get_absolute_url', 'get_all_permissions',
'get_and_d
elete_messages', 'get_full_name', 'get_group_permissions',
'get_next_by_date_joi
ned', 'get_next_by_last_login', 'get_previous_by_date_joined',
'get_previous_by_
last_login', 'get_profile', 'groups', 'has_module_perms', 'has_perm',
'has_perms
', 'has_usable_password', 'id', 'is_active', 'is_anonymous',
'is_authenticated',
'is_staff', 'is_superuser', 'last_login', 'last_name',
'logentry_set', 'message
_set', 'objects', 'password', 'pk', 'prepare_database_save', 'save',
'save_base'
, 'serializable_value', 'set_password', 'set_unusable_password',
'unique_error_m
essage', 'user_permissions', 'username', 'validate_unique']
------------------------------------------------------------------------------------------

i can fin "brukersnarvei" in the attributes, so what am i missing?

the error message:
Brukersnarvei matching query does not exist.

Tom Evans

unread,
Jan 27, 2011, 9:07:48 AM1/27/11
to django...@googlegroups.com
On Thu, Jan 27, 2011 at 2:01 PM, Pacem <meta...@gmail.com> wrote:
>
> i can fin "brukersnarvei" in the attributes, so what am i missing?
>
> the error message:
> Brukersnarvei matching query does not exist.
>

This means that there is no entry in the Brukersnarvei table with that
user_id. If you want to automatically create one of these when you are
creating a user, you need to use a signal, see [1] for an example of
how to automatically create a UserProfile.

Cheers

Tom

[1] http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django

Reply all
Reply to author
Forward
0 new messages