User.get_profile() not working
Django newbie issue. Just trying to understand. I'm setting up a simple UserProfile class to link to User (as described in several places in documentation). Here's what I have --- a simple example of storing the user's website in a profile #In myapp/models.py class UserProfile(models.Model): def __init__(self, website='http://www.default.com'): super(UserProfile, self).__init__() self.website = website user = models.ForeignKey(User, unique=True, related_name="user_profile") <-- note related_name...see below website = models.URLField() #In myproject/settings.py AUTH_PROFILE_MODULE = "myapp.UserProfile" Now I create a empty db run: python2.6 manage.py syncdb All tables look good in postgres. Now in the python shell: Python 2.6.5 (r265:79063, May 12 2010, 10:28:19) >>> from django.contrib.auth.models import User >>> from myapp.models import UserProfile >>> u = User(username='testuser', password='s') >>> u.save() >>> up = UserProfile(website='www.overridethesite.com') >>> up.user=u >>> up.save() So far so good Over in Postgres, everything looks fine: mydb=> select au.id as id, au.username, up.id as upid, up.user_id as fk_userid, up.website from auth_user au join myapp_userprofile up on (au.id=up.user_id); id | username | upid | fk_userid | website --+--+--+---+- 2 | testuser |1 | 2 | www.overridethesite.com (1 row) Now back in the python shell, trying to use the profile from the user obj: >>> u.user_profile.values() <--- works fine using the related_name I set in >>> the UserProfile class [{'website': u'www.overridethesite.com', 'user_id': 2, 'id': 1}] ### But get_profile() fails: >>> myprofile = u.get_profile() Traceback (most recent call last): File "", line 1, in File "/home/myusername/webapps/django_boosters/lib/python2.6/django/ contrib/auth/models.py", line 370, in get_profile self._profile_cache = model._default_manager.using(self._state.db).get(user__id__exact=self.id) File "/home/myusername/webapps/django_boosters/lib/python2.6/django/ db/models/query.py", line 336, in get num = len(clone) File "/home/myusername/webapps/django_boosters/lib/python2.6/django/ db/models/query.py", line 81, in __len__ self._result_cache = list(self.iterator()) File "/home/myusername/webapps/django_boosters/lib/python2.6/django/ db/models/query.py", line 282, in iterator obj = self.model(*row[index_start:aggregate_start]) TypeError: __init__() takes at most 2 arguments (4 given) Can anyone tell me what's going on, or what I'm doing wrong? Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: User.get_profile() not working
Good grief. After spending hours trying to figure out what was wrong, I immediately found the problem after making this post. Here's the solution: I had overridden UserProfile.__init__() like this: class UserProfile(models.Model): def __init__(self, website='http://www.default.com'): while its superclass looks like this: class Model(object): ... def __init__(self, *args, **kwargs): Somewhere deep inside Django the 'model.__init__()' was being called to create a UserProfile passing it a bunch of parameters it was not prepared to accept. So I just changed it to: class UserProfile(models.Model): def __init__(self, website='http://www.default.com', *args, **kwargs): ... and all was well. That explained the 'TypeError: __init__() takes at most 2 arguments (4 given) ' message. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: User.get_profile() not working
Still learning. Always learning. I guess I learn best when I break stuff and do dumb things. Thanks to all who replied for the great advice and tips. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
django-registration question: activation_key_expired.boolean = True
This question refers to line 205 here: http://bitbucket.org/ubernostrum/django-registration/src/b6b213d84d32/registration/models.py In a nutshell: class RegistrationProfile(models.Model): def activation_key_expired(self): ... expiration_date = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS) return self.activation_key == self.ACTIVATED or \ (self.user.date_joined + expiration_date <= datetime.datetime.now()) activation_key_expired.boolean = True # <- what does this do? What does that last line do? I have looked high and low in Django and Python documentation, but I can't find an example or explanation of this pattern. It appears to be clobbering the method 'activation_key_expired()' with a boolean that is always True. Surely there's something else at work here, isn't there? Sorry if this is obvious to everyone but me, but I can't find the answer anywhere. Thanks. Alan btw: James Bennett's django-registration app is great. Elegant, well designed, and does exactly what it needs to. I learned a lot about django and django testing just by reading the code. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: django-registration question: activation_key_expired.boolean = True
> This particular object is used by the admin: > > http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#django.contri... > > Read through that carefully and you'll see the 'boolean' option and a > few other useful tricks. Well, this is embarrassing. I had actually read (err, skimmed?) that page looking for the answer, and did not see the needle in the haystack. After a more careful reading, it's perfectly clear. The attribute name 'boolean' threw me off a bit. I thought maybe there was some Python magic happening related to the fact that the function returned a boolean. Now all is clear. Thanks very much. Alan -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: how to define choices option per model's instance
Here is an approach I have used. I can't tell from your question whether this will help you or not. class AccountEntryModelForm(ModelForm): def __init__(self, organization=None, operation=None, *args, **kwargs): super(ModelForm, self).__init__(*args, **kwargs) self.organization = organization if self.organization: account_entry_types = AccountEntryType.objects.filter(organization=self.organization) self.fields['account_entry_type'].choices = [('', '---')] self.fields['account_entry_type'].choices.extend([(aet.id, aet.name) for aet in account_entry_types]) HTH Alan On Dec 2, 2:00 pm, Alex Boyko wrote: > Hi Cal! > > Thank you very much for your reply and advice, but unfortunately it doesn't > work. It seems like in __init__ method we can't re-assign the choices. Even > if I try: > > class Something(models.Model): > def __init__(self, *args, **kwargs): > _choices = ((SomeValue, 'SomeString'), ) > self.range_marks = models.IntegerField(choices = _choices, > verbose_name = 'Marks of Something') > super(Something, self).__init__(*args, **kwargs) > > In admin page I see that my field has choices option initially defined in > global scope (I've showed that definition in my first letter). > Any suggestions about how I can assign choice attribute on the fly during > instance creation. In future It doesn't change but my instances have to have > different ranges in choices option. > > Best Regards, > Alex > > On 2 December 2010 10:15, Cal Leeming [Simplicity Media Ltd] < > > > > > > > > cal.leem...@simplicitymedialtd.co.uk> wrote: > > Hi Alex, > > > I think this is what you might be looking for. > > > class Something(models.Model): > > def __init__(self, *args, **kwargs): > > if kwargs.has_key('range_marks'): > > _choices = kwargs.get('range_marks') > > del kwargs['range_marks'] > > else: > > _choices = (('default', 'default'), ) > > self.range_marks = models.IntegerField(choices = _choices, > > verbose_name = 'Marks of Something') > > super(Something, self).__init__(*args, **kwargs) > > > Let us know if it works :) > > > Cheers > > > Cal > > > On 02/12/2010 06:53, Alex Boyko wrote: > > >> Hi! > > >> I got such kind of problem. > >> I'd like to define the tuple(list) of choices option at the moment of > >> instance created on the fly > >> In the future these choices will not change. > >> Hence I've found just one way to do it - override __init__() of my model > >> and > >> pass there the particular predefined list of choices that depends on > >> current model's instance (in code - 'range_list') > >> But this approach seems doesn't work properly. > >> Would You be so kind to help with advice - how I can solve my task with > >> dynamical choices per model's instance. > > >> --- > >> RANGE_CHOICES = () > > >> class Something(models.Model): > >> def __init__(self, *args, **kwargs): > >> range = kwargs.pop('range_list', None) > >> super(Covenant, self).__init__(*args, **kwargs) > >> self._meta.get_field_by_name('range_marks')[0]._choices = range > > >> range_marks = models.IntegerField(choices = RANGE_CHOICES, verbose_name > >> = 'Marks of Something') > >> - > > >> Thanks in Advance! > >> Best Regards! > >> Alex > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Django users" group. > >> To post to this group, send email to django-us...@googlegroups.com. > >> To unsubscribe from this group, send email to > >> django-users+unsubscr...@googlegroups.com >> groups.com> > >> . > >> For more options, visit this group at > >>http://groups.google.com/group/django-users?hl=en. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: django noob question
Without knowing your problem domain, you might want to start off with: class League(models.Model): name = models.CharField(max_length=200) class Team(models.Model): name = models.CharField(max_length=200) league = models.ForeignKey(League) Generally you don't want a model with a plural name (like 'Leagues'). When you're dealing with more than one league, you will get leagues out of your League model with: my_leagues = League.objects.all() which will return a queryset with all your leagues. Likewise, for teams: my_teams = Team.objects.all() If you want to know the league for a specific team: my_team = Team.objects.get(pk=1) my_team_league = my_team.league HTH, Alan On Jan 26, 2:37 pm, yousuf wrote: > this is my models.py > > from django.db import models > > # Create your models here. > > class Leagues(models.Model): > LeagueName = models.CharField(max_length=200) > > class Team(models.Model): > TeamName = models.CharField(max_length=200) > > class LeagueTable(models.Model): > league = models.ForeignKey(Leagues) > team = models.CharField(max_length=200) > matches_played = models.IntegerField() > matches_won = models.IntegerField() > matches_drawn = models.IntegerField() > matches_lost = models.IntegerField() > points = models.IntegerField() > > class FixtureTable(models.Model): > league = models.ForeignKey(Leagues) > fixture_date = models.DateField('Fixture Date') > team_one = models.CharField(max_length=200) > team_one_score = models.IntegerField() > team_two = models.CharField(max_length=200) > team_two_score = models.IntegerField() > in the "class FixtureTable", i want team_one and team_two to be linked > to two differnt teams in "class Team" models. how to create multiple > relations, or is it possible. > > PS: this is purely a noob, with a little experience in programming, > but no experience with either python or databases. > > PPS: I found that the best way to learn is to create your own problem, > and solve the problem in parallel while following the tutorial. so i > am just stuck in the beginning. thankyou. > > thankyou. -- 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 at http://groups.google.com/group/django-users?hl=en.
Re: django noob question
I forgot to address the issue of your LeagueTable, which appears to be a way to keep track of standings. It looks like what you're really doing is trying to keep track of matches won and lost, points earned, etc i.e., the standings of a league. One way to do this is with a Match model: class Match(models.Model): date = models.DateField() home_team = models.ForeignKey(Team) visiting_team = models.ForeignKey(Team) home_score = models.IntegerField() visiting_score = models.IntegerField() For each match you would create an object in your Match table, with the score for each team. Having this data would allow you to calculate 1) which team won, 2) number of points earned, etc. So just querying the Match table would allow you to calculate on-the- fly the record for each team, the number of points, then sort by the number of points to produce a Standings queryset. I hope that makes sense without giving examples of all the queries. This is just one way to do what I think you're trying to do. It may not be the best way. But hopefully it gives you some ideas about how to use Django models. Alan On Jan 27, 4:03 pm, adj7388 wrote: > Without knowing your problem domain, you might want to start off with: > > class League(models.Model): > name = models.CharField(max_length=200) > > class Team(models.Model): > name = models.CharField(max_length=200) > league = models.ForeignKey(League) > > Generally you don't want a model with a plural name (like 'Leagues'). > When you're dealing with more than one league, you will get leagues > out of your League model with: > > my_leagues = League.objects.all() > > which will return a queryset with all your leagues. > > Likewise, for teams: > > my_teams = Team.objects.all() > > If you want to know the league for a specific team: > > my_team = Team.objects.get(pk=1) > my_team_league = my_team.league > > HTH, > Alan > > On Jan 26, 2:37 pm, yousuf wrote: > > > > > > > > > this is my models.py > > > from django.db import models > > > # Create your models here. > > > class Leagues(models.Model): > > LeagueName = models.CharField(max_length=200) > > > class Team(models.Model): > > TeamName = models.CharField(max_length=200) > > > class LeagueTable(models.Model): > > league = models.ForeignKey(Leagues) > > team = models.CharField(max_length=200) > > matches_played = models.IntegerField() > > matches_won = models.IntegerField() > > matches_drawn = models.IntegerField() > > matches_lost = models.IntegerField() > > points = models.IntegerField() > > > class FixtureTable(models.Model): > > league = models.ForeignKey(Leagues) > > fixture_date = models.DateField('Fixture Date') > > team_one = models.CharField(max_length=200) > > team_one_score = models.IntegerField() > > team_two = models.CharField(max_length=200) > > team_two_score = models.IntegerField() > > in the "class FixtureTable", i want team_one and team_two to be linked > > to two differnt teams in "class Team" models. how to create multiple > > relations, or is it possible. > > > PS: this is purely a noob, with a little experience in programming, > > but no experience with either python or databases. > > > PPS: I found that the best way to learn is to create your own problem, > > and solve the problem in parallel while following the tutorial. so i > > am just stuck in the beginning. thankyou. > > > thankyou. -- 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 at http://groups.google.com/group/django-users?hl=en.