Your Ad Here

Wednesday, May 30, 2012

Re: A signal, or equivalent, for when all apps are loaded?

On Wed, May 30, 2012 at 9:59 PM, Bill Freeman <ke1g.nh@gmail.com> wrote:
> On Tue, May 29, 2012 at 7:35 PM, Russell Keith-Magee
> <russell@keith-magee.com> wrote:
>> On Wed, May 30, 2012 at 4:42 AM, Bill Freeman <ke1g.nh@gmail.com> wrote:
>>> I continue to struggle with occasional circular import problems
>>> (caused by a combination of haystack, filebrowser, and a model which
>>> both has a search_indexes.py and a model that has a filebrowser
>>> field).
>>>
>>> I had thought that I had patched the issue by importing haystack in my
>>> root urlconf (the problem doesn't happen under the development server
>>> because it "validates models" early, but the import order was
>>> different in production).  But: 1, I kept having to add "import
>>> haystack" to my management commands; and 2, it still occasionally
>>> fails on restart (a timing hazard of some kind).
>>>
>>> It strikes me that one fix would be to defer haystack's scan of the
>>> INSTALLED_APPS until all the apps have been loaded.  But when is that?
>>>  I can ask django.db.models.loading.app_cache_ready(), but it will say
>>> False when haystack (or its models.py, which it has, despite not
>>> having any models) is being imported (since the list of apps being
>>> cached can't be finished until haystack.models import has finished).
>>> So when could I run it again?  It would be nice to register for an
>>> "apps are loaded" signal, but I don't see on in the docs, and there is
>>> no evidence of sending one in django.db.models.loading.AppCache.
>>>
>>> Is there a right way to get something called after all the models have
>>> been imported?  (Django 1.3)
>>
>> Unfortunately, there isn't. This is a long standing problem, and one
>> that the app-refactor was looking into; however, that effort hasn't
>> come to fruition. Django's startup sequence is an area where some work
>> is required. The closest thing to a "signal" you're going to get is to
>> hook into the URL configuration, in the same way the admin
>> registration process does -- but it looks like you've tried that and
>> haven't had any luck.
>>
>> One trick that I'm aware of that *might* work in your case is to force
>> the validation code to run in your production stack -- for example, as
>> a call in your WSGI container. From a functional perspective, this is
>> an expensive no-op. The validation won't ever return a different
>> result, which is why Django's WSGI handler doesn't call validate.
>> However, the process of validation *does* cause the startup sequence
>> to be a little more predictable, because the validation mechanism
>> imports *all* your app modules in your project in a predictable order.
>> If your problem is being caused by an unpredictable import order,
>> forcing a specific import order may work around your problem (or at
>> least make the side effects more predictable, and match the behaviour
>> of the development server).
>>
>> Yours
>> Russ Magee %-)
>
> Thanks Russ.
>
> I guess that I could just try importing django.db.models in my wsgi
> file (in turn imports django.db.models.loading, which initializes the
> AppCache) as a validation equivalent?

I mentioned the validation thing because I know that it works. I'd
have to check the models import to make sure it has exactly the same
side effects, but it stands to reason that it might.

Yours,
Russ Magee %-)

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Re: Foreign Key Chicken/Egg Inline Fu

Thanks Kurtis!

That is helpful.

-Patrick

On May 30, 1:14 pm, Kurtis Mullins <kurtis.mull...@gmail.com> wrote:
> I just checked on my own code that does something in a different
> context but kinda performs the same check. I thought I'd share it with
> you in case it helps out at all.
>
>     def clean_title(self):
>
>         # Grab the data to verify this Title is unique to this user.
>         title = self.cleaned_data['title']
>         owner = self.request.user
>         slug = slugify(title)
>
>         # Grab all user pages query for validation
>         user_pages = AbstractPage.objects.filter(owner = owner)
>         user_pages = user_pages.exclude(pk = self.instance.pk)
>
>         # Validate Uniqueness
>         if user_pages.filter(title = title).exists():
>             raise ValidationError("The page title must be unique. It appears "
>                                   + "you already have a page with this title.")
>         elif user_pages.filter(slug = slug).exists():
>             raise ValidationError("It appears that you're trying to use two "
>                                   + " pages with titles that are too similar.")
>         else: return title
>
> Of course, you'd want to 'clean' the Captain boolean field and make
> sure that no other player in the same team is a captain before
> allowing it to pass. Different context, but hopefully this approach
> can help.
>
> On Wed, May 30, 2012 at 4:05 PM, Kurtis Mullins
>
>
>
>
>
>
>
> <kurtis.mull...@gmail.com> wrote:
> > Sorry, I completely mis-read the last part of your problem. You
> > already thought about the same solution, haha. You might be able to
> > modify the Player model's clean (or save) method to prohibit any Team
> > from having more than one team captain. However, I'd probably just
> > check it in the Form you're using (outside of Admin)
>
> > On Wed, May 30, 2012 at 4:02 PM, Kurtis Mullins
> > <kurtis.mull...@gmail.com> wrote:
> >> Unless a player can play for multiple teams (which I'm doubting since
> >> Team is a ForeignKey for a Player), why not remove that 'captain'
> >> attribute from your Team and put it into your Player model as a
> >> boolean field? You could create a ModelManager or class-level model
> >> method to grab the associated team caption if you want to access it
> >> easily. Otherwise, it's just a matter of:
>
> >> Team.objects.get('team_name').player_set.filter(captain=True) # Or
> >> something along those lines...
>
> >> Just an idea anyways :) Good luck!
>
> >> On Wed, May 30, 2012 at 2:15 PM, Patrick Gavin <wezel...@gmail.com> wrote:
> >>> Hi-
>
> >>> I'm new to Django, but so far I think it is the bee's knees.
>
> >>> I could use some insight into a problem I'm working on.
>
> >>> Given the following...
>
> >>> ---------------------------------
> >>> # models.py
>
> >>> from django.db import models
>
> >>> class Team(models.Model):
> >>>    name = models.CharField(max_length=30)
>
> >>>    # captain is a player- but there can only be one captain per team.
> >>>    # null and blank are true to allow the team to be saved despite
> >>> not having a captain
> >>>    captain = models.ForeignKey('Player', related_name='team_captain',
> >>> null=True, blank=True)
>
> >>>    def __unicode__(self):
> >>>        return u'{0}'.format(self.name)
>
> >>> class Player(models.Model):
> >>>    first_name = models.CharField(max_length=20)
> >>>    last_name = models.CharField(max_length=30)
> >>>    team = models.ForeignKey('Team')
>
> >>>    def __unicode__(self):
> >>>        return u'{0} {1}'.format(self.first_name, self.last_name)
>
> >>> # admin.py
>
> >>> from league.models import *
> >>> from django.contrib import admin
>
> >>> class PlayerInline(admin.TabularInline):
> >>>    model = Player
> >>>    extra = 8
>
> >>> class TeamAdmin(admin.ModelAdmin):
> >>>    inlines = [PlayerInline]
> >>>    list_display = ('name', 'captain')
>
> >>> ---------------------------------
>
> >>> As is, when I create a new team in the admin interface I can add the
> >>> players inline, but I have to leave the team captain blank, save the
> >>> team, and then go back and change the team and set the captain to one
> >>> of the players added previously. This is kind of a drag though.
>
> >>> Is there a way that I can make the team captain be automatically set
> >>> to the first player entered inline when the team is saved the first
> >>> time?
>
> >>> I 've thought about adding a boolean field to the Player model called
> >>> "is_captain", but I want to enforce only one captain per team. If I
> >>> were to go this route, is there a way I could make the is_captain
> >>> field default to True for the first inline entry but false for the
> >>> rest?
>
> >>> Thanks,
>
> >>> -Patrick
>
> >>> --
> >>> 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+unsubscribe@googlegroups.com.
> >>> For more options, visit this group athttp://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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Re: Adding values to formdata of ModelForm before saving

Yes, it is possible:

obj = form.save(commit=False)
obj.in_calender-id = kalender
obj.save()


On Wed, May 30, 2012 at 2:56 PM, Schmidtchen Schleicher <spiollinux@googlemail.com> wrote:
Is it possible to put missing data into the data of a form before calling form.save() ?
I want to add the calendar-id (calid/in_calendar) before saving to the model.

 http://susepaste.org/23451355

I tried adding it after instantiating the form with form.in_calendar_id = kalender but it didn't work
I don't want to use hidden fields because manipulating them is easy

Maybe https://groups.google.com/forum/?fromgroups#!topic/django-users/F5yHH-G5QLM describes how to solve it but I didn't understand it.

PS: If you find very ugly code please correct me -- I'm a beginner

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/v0GYTeLYPAsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
Jonathan D. Baker
Developer
http://jonathandbaker.com

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Adding values to formdata of ModelForm before saving

Is it possible to put missing data into the data of a form before calling form.save() ?
I want to add the calendar-id (calid/in_calendar) before saving to the model.

 http://susepaste.org/23451355

I tried adding it after instantiating the form with form.in_calendar_id = kalender but it didn't work
I don't want to use hidden fields because manipulating them is easy

Maybe https://groups.google.com/forum/?fromgroups#!topic/django-users/F5yHH-G5QLM describes how to solve it but I didn't understand it.

PS: If you find very ugly code please correct me -- I'm a beginner

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/v0GYTeLYPAsJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Re: Django ModelForm user best practices

For example on second method i can change post, board id's via html,
and write to board where i can be banned.
I think we shoudn't override standart methods if we con don't override
them(this about third method)

P.S. sorry for my terrible english.

2012/5/31 Kurtis Mullins <kurtis.mullins@gmail.com>:
>> On second method some experience users can
>> override hidden data
>
> For the second method, you'd just use -- class Meta: fields =
> ('board', 'post', 'name') to prohbit anyone from trying to override
> the 'user', if that's what you're talking about.
>
>> And it's a bad idea to
>> override __init__ and save method in my humble opinion/
>
> What better purpose for the save() method than to save? :) I'm not
> sure why it's a bad idea to override __init__, though. Feel free to
> elaborate. I'm always open for new information!
>
> --
> 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+unsubscribe@googlegroups.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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Re: Python IDLE

Back to the original question,

Did you try running "python manage.py runserver" from the command
prompt? As far as changing those icons back to Python, I believe you
have to change your 'Default Program'. I'm not sure which version of
Widnows you're using (or even how to do it in Windows, I'm running
Linux) but I'm sure you could use Google (or duckduckgo.com) for a
quick answer there.

If you still can't run 'runserver', try using your command prompt to
do something along the following (your paths may/will be different)

c:\python27\bin\python.exe c:\my_directory\my_project\manage.py runserver

Basically, you're just telling the Python interpreter (python.exe or
python27.exe) to execute the "manage.py" file. Also, please note:
Python Versions > 2.7.x (3+) will not work with Django at the moment.

Good luck!

On Mon, May 28, 2012 at 8:37 AM, coded kid <duffleboi911@gmail.com> wrote:
> I'm in a big mess now, I've lost my projects due to this errror. I'm
> on windows, This is how I encounter the problem; I try to edit my
> settings.py in IDLE. After right clicking on the files, I choose open
> program with these default file. I choose idle window bat file, and I
> clicked Ok. It didn't open, I try to run manage.py runserver on my
> DOS. Not working, it will pop up the IDLE Shell and mange.py script by
> displaying it in IDLE. It didn't run the server. The logo of my python
> files have changed. How can I revert it back to open with IDLE? And
> use it as default for my python script?
>
> --
> 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+unsubscribe@googlegroups.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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Re: Foreign Key Chicken/Egg Inline Fu

I just checked on my own code that does something in a different
context but kinda performs the same check. I thought I'd share it with
you in case it helps out at all.

def clean_title(self):

# Grab the data to verify this Title is unique to this user.
title = self.cleaned_data['title']
owner = self.request.user
slug = slugify(title)

# Grab all user pages query for validation
user_pages = AbstractPage.objects.filter(owner = owner)
user_pages = user_pages.exclude(pk = self.instance.pk)

# Validate Uniqueness
if user_pages.filter(title = title).exists():
raise ValidationError("The page title must be unique. It appears "
+ "you already have a page with this title.")
elif user_pages.filter(slug = slug).exists():
raise ValidationError("It appears that you're trying to use two "
+ " pages with titles that are too similar.")
else: return title

Of course, you'd want to 'clean' the Captain boolean field and make
sure that no other player in the same team is a captain before
allowing it to pass. Different context, but hopefully this approach
can help.

On Wed, May 30, 2012 at 4:05 PM, Kurtis Mullins
<kurtis.mullins@gmail.com> wrote:
> Sorry, I completely mis-read the last part of your problem. You
> already thought about the same solution, haha. You might be able to
> modify the Player model's clean (or save) method to prohibit any Team
> from having more than one team captain. However, I'd probably just
> check it in the Form you're using (outside of Admin)
>
> On Wed, May 30, 2012 at 4:02 PM, Kurtis Mullins
> <kurtis.mullins@gmail.com> wrote:
>> Unless a player can play for multiple teams (which I'm doubting since
>> Team is a ForeignKey for a Player), why not remove that 'captain'
>> attribute from your Team and put it into your Player model as a
>> boolean field? You could create a ModelManager or class-level model
>> method to grab the associated team caption if you want to access it
>> easily. Otherwise, it's just a matter of:
>>
>> Team.objects.get('team_name').player_set.filter(captain=True) # Or
>> something along those lines...
>>
>> Just an idea anyways :) Good luck!
>>
>> On Wed, May 30, 2012 at 2:15 PM, Patrick Gavin <wezelboy@gmail.com> wrote:
>>> Hi-
>>>
>>> I'm new to Django, but so far I think it is the bee's knees.
>>>
>>> I could use some insight into a problem I'm working on.
>>>
>>> Given the following...
>>>
>>> ---------------------------------
>>> # models.py
>>>
>>> from django.db import models
>>>
>>> class Team(models.Model):
>>>    name = models.CharField(max_length=30)
>>>
>>>    # captain is a player- but there can only be one captain per team.
>>>    # null and blank are true to allow the team to be saved despite
>>> not having a captain
>>>    captain = models.ForeignKey('Player', related_name='team_captain',
>>> null=True, blank=True)
>>>
>>>    def __unicode__(self):
>>>        return u'{0}'.format(self.name)
>>>
>>> class Player(models.Model):
>>>    first_name = models.CharField(max_length=20)
>>>    last_name = models.CharField(max_length=30)
>>>    team = models.ForeignKey('Team')
>>>
>>>    def __unicode__(self):
>>>        return u'{0} {1}'.format(self.first_name, self.last_name)
>>>
>>>
>>> # admin.py
>>>
>>> from league.models import *
>>> from django.contrib import admin
>>>
>>> class PlayerInline(admin.TabularInline):
>>>    model = Player
>>>    extra = 8
>>>
>>> class TeamAdmin(admin.ModelAdmin):
>>>    inlines = [PlayerInline]
>>>    list_display = ('name', 'captain')
>>>
>>> ---------------------------------
>>>
>>> As is, when I create a new team in the admin interface I can add the
>>> players inline, but I have to leave the team captain blank, save the
>>> team, and then go back and change the team and set the captain to one
>>> of the players added previously. This is kind of a drag though.
>>>
>>> Is there a way that I can make the team captain be automatically set
>>> to the first player entered inline when the team is saved the first
>>> time?
>>>
>>> I 've thought about adding a boolean field to the Player model called
>>> "is_captain", but I want to enforce only one captain per team. If I
>>> were to go this route, is there a way I could make the is_captain
>>> field default to True for the first inline entry but false for the
>>> rest?
>>>
>>> Thanks,
>>>
>>> -Patrick
>>>
>>> --
>>> 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+unsubscribe@googlegroups.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-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.