Re: Difficulties displaying correct value in select widget with ModelForm/formset_factory:

2013-05-10 Thread Matt Conrad
Answering my own question from a few days ago, this:

choice_vals = scene.begin_scene.all().values()
ChoiceFormFactory = formset_factory(ChoiceForm)
formset = ChoiceFormFactory(initial=choice_vals)

is all wrong. I needed to be using a modelformset_factory, not a
formset_factory, with a few extra params:

ChoiceFormSetFactory = modelformset_factory(Choice, form=ChoiceForm,
extra=0)
formset = ChoiceFormSetFactory(queryset=scene.begin_scene.all())

This assigns the value of the select widget correctly out of the box, and
also avoids some later issues I ran into with .values() and initial=.

Matt







On Mon, May 6, 2013 at 4:28 PM, Matt C  wrote:

> I'm resorting to code in __init__ to set my select dropdown properly when
> using a ModelForm with formset_factory, and wondering if there is a better
> way.
>
> I have a model that looks like this:
>
> class Choice(models.Model):
> desc = models.TextField()
> begin_scene = models.ForeignKey(Scene, related_name='begin_scene')
> end_scene = models.ForeignKey(Scene, related_name='end_scene')
>
> with a corresponding ModelForm.
>
> My view contains this:
>
> choice_vals = scene.begin_scene.all().values()
> ChoiceFormFactory = formset_factory(ChoiceForm)
> formset = ChoiceFormFactory(initial=choice_vals)
>
>
> When I render the form, I get a select dropdown which contains correct FK
> values from Scene. However, the correct option in the dropdown is not
> selected.
>
> I did a bunch of searching and playing around and ended up with this in my
> form class, which does work to set the selected option correctly.
>
> class ChoiceForm(ModelForm):
> def __init__(self, *args, **kwargs):
> if 'initial' in kwargs and 'end_scene_id' in kwargs['initial']:
> kwargs['initial']['end_scene'] =
> kwargs['initial']['end_scene_id']
> super(ChoiceForm, self).__init__(*args, **kwargs)
>
> But I'm wondering if I missed something simpler/cleaner/more idiomatic.
> Did I?
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Fwd: Django in production on Windows

2010-12-03 Thread Matt Conrad
I notice that the conversation is mostly about IIS vs Apache.

You'll also want to think about the database. Django doesn't have
built in support for MSSQL. You could run Postgres or MySQL on
Windows, or there are some adapters for Django that will let it talk
to SQL Server. Either way, you'll probably want to think about this up
front.

I haven't got any experience with this, but others here probably do.
If you want to stick with MSSQL, here's a discussion from a year ago
on SO to get you started:
http://stackoverflow.com/questions/842831/using-sql-server-with-django-in-production

Matt


On Fri, Dec 3, 2010 at 5:32 AM, ashdesigner  wrote:
> Ok, I see. Happily, this is not the case with us. We've discussed the
> issue with the crew, and our decision is that most likely we'll deploy
> on Win+Apache.
> Hope there won't be any considerable development limitations due to
> the use of the Windows Server OS.
>
> Thank you a lot!
>
> Regards,
> Anthony

-- 
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: Using formsets with multiple models

2010-11-01 Thread Matt Conrad
On Fri, Oct 29, 2010 at 11:58 AM, Bill Brigham  wrote:

> On a questionnaire, you have one or more questions which will require
> either an essay answer or multiple choice answers. When creating a new
> questionnaire, I would like to show a textarea for the Question.text
> common to both types, a radio select for the Question.question_type,
> and then present either the EssayQuestion.initial_answer textarea or
> the inputs for adding multiple choice options. The user can click "add
> question" to append another form beneath to enter a new question. It's
> only on the final "save questionnaire" that I handle the POST and
> save.

First, let me say that I'm still learning Django myself, and don't
understand formsets especially well. There might be an easy and/or
elegant way to handle this with formsets that I don't know.

That said, if you are only entering new questions one by one, it sure
sounds easier to me to POST and save each new question individually.
If there aren't many questions per questionaire, it probably wouldn't
hurt to post/refresh the whole page each question. If there are a lot
of questions, you could use javascript to make the building process
smoother for the user.

> I have spent a few days now trying different permutations of
> formsets, modelformsets and inline_formsets but to no avail. The
> closest I've got is to create a QuestionFormset, EssayQuestionFormset
> and a MultipleChoiceQuestionFormset, then keep track of which form
> belongs to which question when saving. I'm not sure how to reconstruct
> the forms should it raise validation errors though.
>
> Another option is to create a QuestionForm containing fields for 0..N
> possible fields, i.e. multiplechoice_choice_0,
> multiplechoice_correct_0, multiplechoice_choice_1,... But this feels
> wrong to me.

Me too.

> It seems overly complicated which makes me think I'm going about it
> the wrong way. Any help would be much appreciated please!

If you can do it, handling one question at a time will be easier. And
it will be easier to maintain when you have to add a new question type
of checkbox, dropdown list, or whatever it is they'll want next.  :)

Matt

-- 
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.



Newbie Django project, suggestions for code improvement?

2009-08-26 Thread Matt Conrad

I wrote a little concentration game in Django and put it up on Google
App Engine:

http://matt-scratch.appspot.com/

I'm sure there are some awkward places in the code where I could
leverage Django better.  If anyone is curious and would like to
comment or make suggestions, the source code (Django/SQLite version)
is here:

http://matt-scratch.appspot.com/downloads/ccsite-1-01.zip

If you download it, you'll want to visit /init_colors/ first thing
after running syncdb.  The game won't be playable until the colors are
initialized.  You'll also need to customize settings.py.

The part of the project that bugs me the most is the URL routing.  It
seems awkward to have a matching URL for each view.  I bet there is a
tidier way.  There may be other obvious Djangonic improvements I don't
see yet.

Any suggestions appreciated.

Matt

--~--~-~--~~~---~--~~
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: Help: Running 1.0 and 0.96 side by side?

2008-09-13 Thread Matt Conrad

On Fri, Sep 12, 2008 at 1:58 PM, Kevin Teague <[EMAIL PROTECTED]> wrote:

> Finally, you could patch a Python program such as manage.py so that
> you do something like:
>
> import sys
> sys.path[0:0] = [
>  '\somelocation-for-django-1.0\',
>  ]
> import django
>
> This would pick up your Django 1.0 install explicitly without relying
> on setting the PYTHONPATH (since setting the PYTHONPATH can effect
> imports for any Python program run under a user account). Of course,
> hand-patching your Python scripts in this manner is incredibly lame,

I'm a beginner, lame is OK as long as I'm learning.  :)

Matt

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help: Running 1.0 and 0.96 side by side?

2008-09-12 Thread Matt Conrad

On Fri, Sep 12, 2008 at 11:15 AM, Steve Holden <[EMAIL PROTECTED]> wrote:
>
> Matt Conrad wrote:
>> 2008/9/12 Jarek Zgoda <[EMAIL PROTECTED]>:
>>> Use virtualenv (http://pypi.python.org/pypi/virtualenv) with separate
>>> environments for each django version.
>>
>> Thanks Jarek.  I will try the directory based approach first, but also
>> keep this in mind.
>>
> OK, here's why you don't want to try the directory-based approach: all
> the modules in django96 have imports from django in them, so they will
> pick up the 1.0 modules. This will lead to what might be called "a ugly
> mess", avoidable only by changing them to imports from django96.
>
> Virtualenv will give you a much cleaner separation between the two
> environments.

I don't doubt you are correct and I'll end up going to virtualenv
eventually, but now I'm feeling stubborn and curious about directories
+ PYTHONPATH per my conversation with Jeff.  Thanks for the
explanation.

Matt

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help: Running 1.0 and 0.96 side by side?

2008-09-12 Thread Matt Conrad

On Fri, Sep 12, 2008 at 11:15 AM, Jeff Anderson
<[EMAIL PROTECTED]> wrote:

> You can influence what this list is, by setting the PYTHONPATH
> environmental variable. All you need to do is set your PYTHONPATH
> appropriately, and point it to the appropriate place, depending on which
> Django installation you need to use. Anything in the PYTHONPATH
> environmental variable is put at the beginning of sys.path in the python
> interpretor.
>
> You can't just rename the django directory and expect it to work-- it won't.

Thanks for the PYTHONPATH tutorial.  I notice that
\site-packages\django is not explicitly listed, though other
subdirectories of \site-packages are.  I assume that there is an
implied search of subdirectories w/ PYTHONPATH and that is why Django
still runs.

If I understand correctly then, I would want to rename my 0.96 django
directory to "django096", move it OUT of sitepackages so it isn't
found accidentally, install django version 1.0.  Now I have \django
(1.0) under sitepackages and 0.96 elsewhere.  Using a "from django"
import statement will give me 1.0 as default.

If I wanted to use 0.96 instead, I would need to set my PYTHONPATH env
var to the copied \django096 directory.  When I start Python/Django,
this directory would be inserted at the beginning of the sys.path
list, so Python would now use \django096 preferentially to
\site-packages\django, giving me v0.96 behavior.  My import statements
would still be "from django" and not "from django096".

Because all of the django imports will be detected first in the
\django096 directory, using "from django" will not burn me, even with
the nested import statements that Steve Holden was talking about.  (I
could imagine other reasons to get burned, but you say this has worked
well for you in the past, so I'm assuming they don't happen.)

Do I have the above correct?

Matt

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help: Running 1.0 and 0.96 side by side?

2008-09-12 Thread Matt Conrad

2008/9/12 Jarek Zgoda <[EMAIL PROTECTED]>:
>
> Use virtualenv (http://pypi.python.org/pypi/virtualenv) with separate
> environments for each django version.

Thanks Jarek.  I will try the directory based approach first, but also
keep this in mind.

Matt

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Help: Running 1.0 and 0.96 side by side?

2008-09-12 Thread Matt Conrad

Thanks for the reply. I don't quite understand yet.  Let's see how close I am.

I have a Python application directory (on my machine,
C:\apps\Python25\).  Inside that directory I have
\Lib\site-packages\django, which is currently v0.96.

Right now, the PYTHONPATH environment variable is not set at all on my
machine.  When I work through Django examples, I can run "manage.py"
from my Django application folder (C:\work\djcode\mytut\) and
automatically talk to django modules, which are presumably in
\Lib\site-packages\django.  This happens, I assume, because
C:\apps\Python25\ is on my system path, and either a) Python
automatically checks the \site-packages\ subdirectory for imports, or
b) a hook was added when I installed Django, so that Python knows to
look there for "django" imports.

I have not installed Django v1.0 yet.  I will have to do something
when I install it so as not to overwrite the version already in
\site-packages\.  The obvious solution is to rename the existing
directory to (say) django096 before installing, but I don't know what
I might break when I do that.

I'm not sure we're on the same page, but maybe you are telling me that
if I rename the old directory and then install v1.0 so that I have
both \django (1.0) and \django096 (0.96) both under \site-packages\,
that I can set the PYTHONPATH variable to tell python to
preferentially choose one of these directories over the other?  Or are
you perhaps talking about having different installs of Python itself,
or something else entirely?

Matt


On Fri, Sep 12, 2008 at 10:09 AM, Jeff Anderson
<[EMAIL PROTECTED]> wrote:
> Matt wrote:
>> Does anyone have ideas on running 0.96 and 1.0 side by side safely?
>>
> All I've done in the past is set my PYTHONPATH based on which
> checkout/release of Django I want used in a particular instance. It
> works quite well, even in production.
>
> Jeff Anderson
>
>

--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---