Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 27, 2011 12:19:37

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

object has no attribute


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!

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 27, 2011 12:22:10

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

object has no attribute


On Thu, Jan 27, 2011 at 9:41 AM, Pacem <metapa...@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

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 27, 2011 14:01:50

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

object has no attribute


> 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 = User.objects.get(id=1)
>>> dir(user)

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

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

the error message:
Brukersnarvei matching query does not exist.

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 27, 2011 14:08:00

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

object has no attribute


On Thu, Jan 27, 2011 at 2:01 PM, Pacem <metapa...@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 for an example of
how to automatically create a UserProfile.

Cheers

Tom

http://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 24th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support