Tuesday, November 1, 2011

Re: Set of choices of which several can be chosen without using a ManyToManyField

As alluded to previously, the most "straightforward way to use a set of choices of which several can be chosen" IS to use a ManyToManyField. The syntax is slightly different, but ManyToManyFields are really easy to use with Django. Do not reinvent the wheel in this case.

Thanks,

Mark Furbee


On Tue, Nov 1, 2011 at 7:49 AM, J. Cliff Dyer <jcd@sdf.lonestar.org> wrote:
On 11/01/2011 09:05 AM, Jaroslav Dobrek wrote:
You are confusing model fields with form fields. MultipleChoiceField
is a form field, not a model field.
I wasn't aware of the existence of MultipleChoiceFields. The idea of
the above code was to express that I wanted to use this code

class Candidate(models.Model):

    programming_languages = models.CharField(max_length=50, choices=(

            (u'Python)', u'Python'),
            (u'C++', u'C++'),
            (u'Java', u'Java'),
            # ...
            ), blank=True)

with the only exception that, in the admin interface, several choices
are possible when one creates a new candidate object. I.e. I want
admins to be able to create a candidate that knows, say Python *and* C+
+ by choosing both of these languages during the creation of the
object. I used the string "MultipleChoiceField" as a dummy for
whatever should be used instead.

Jaroslav






If you want a field that will be represented by a MultipleChoiceField
in model, you simply need to define 'choices' on a field class.

https://docs.djangoproject.com/en/1.3/ref/models/fields/#choices

Cheers

Tom
Still, you want to control the input at the form level, not the model level.  Create a ModelForm for your Candidate, but override the language field to take a MultipleChoiceField, and then override the __init__() and save() methods to handle them properly.  "Properly," of course, is determined by your application, and how you want to store the information in the database.  You could choose to store it in a CharField as a comma separated list of language names, or in an IntegerField as a bit field (0x1 = 'Python', 0x2 = 'Perl', 0x4 = PHP, so 0x5 means the user knows Python and PHP, but not Perl).  The latter is a more efficient way to store the data, but the former is arguably more human-friendly.

Ultimately, though, it seems that you are adding complexity rather than removing it.  This is exactly what many to many relationships are designed to handle.  If you want to use another method, you have to figure out the details for yourself.

Cheers,
Cliff


--
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: django model forms validation and error display

I recommend using Class Based Views if you're running Django 1.3. I tried to go over your app and re-create a prototype using CBVs. I didn't spend a lot of time on the actual logic or field types. I also didn't even try running this. You'll need to fill in a couple of blanks but hopefully you get the idea. I hope it helps :)

http://dpaste.com/hold/645547/

On Tue, Nov 1, 2011 at 11:39 AM, luke lukes <lordluke80@gmail.com> wrote:
i have this template: http://dpaste.com/645468/ that send data to this
view: http://dpaste.com/645469/. the template display a form made by
modelforms, with a custom layout. i have to validate the fields value
send to view, and then redirect to the same template if the field
validation fails. i've tried to do it by creating an instance of the
related model, then gathering all fields one by one (checking before
if they are present in request.POST) and assigning it to the model
field, and then assigning that model instance to a new modelform
instance. then i've prefromed a .is_valid() check, but it fails since
it require the model instance pc, so that model instance need to exist
on db. so how can i validate those data, and if validation fails,
redirect to another template and display the errors?

--
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: Django app, part 1, error with the database

'NAME': 'C:/Users/songbird/Desktop/bdd/' # Or path to database file
if using sqlite3.

You're using a SQLite database, so you've to provide the full path to
SQLite database file, for example: 'NAME':
'C:/Users/songbird/Desktop/bdd/mydb.db3' instead only the path to
directory.

2011/11/1 Nikolas Stevenson-Molnar <nik.molnar@consbio.org>:
> Hi Nicolas,
>
> It looks like you have a syntax error in your settings file. It's
> possible you left of a comma after one of your dictionary values, or
> didn't close a brace. I can't tell for sure without more context: could
> you please provide the complete "DATABASES" section of your settings file?
>
> On 11/1/2011 9:46 AM, Nicolas wrote:
>> Dear all,
>> I'm completly new to the world of django and python.
>> I'm trying to go through the tuto "Writing your first Django app, part
>> 1" and of course I have an error.
>> This error happens in the step "database setup". I edited the settings
>> file, wrote 'django.db.backends.sqlite3' for engine and a path to a db
>> file (as it's said in the tuto that "If the file doesn't exist, it
>> will automatically be created when you synchronize the database for
>> the first time" I just set a path to an empty folder).
>> And then, I run manage.py syncdb and I get this :
>>
>> C:\Python27\Lib\site-packages\django\bin\mysite>manage.py syncdb
>> Traceback (most recent call last):
>>   File "C:\Python27\Lib\site-packages\django\bin\mysite\manage.py",
>> line 9, in <
>> module>
>>     execute_from_command_line(sys.argv)
>>   File "C:\Python27\lib\site-packages\django\core\management
>> \__init__.py", line
>> 422, in execute_from_command_line
>>     utility.execute()
>>   File "C:\Python27\lib\site-packages\django\core\management
>> \__init__.py", line
>> 361, in execute
>>     self.fetch_command(subcommand).run_from_argv(self.argv)
>>   File "C:\Python27\lib\site-packages\django\core\management
>> \__init__.py", line
>> 234, in fetch_command
>>     app_name = get_commands()[subcommand]
>>   File "C:\Python27\lib\site-packages\django\core\management
>> \__init__.py", line
>> 102, in get_commands
>>     apps = settings.INSTALLED_APPS
>>   File "C:\Python27\lib\site-packages\django\utils\functional.py",
>> line 185, in
>> inner
>>     self._setup()
>>   File "C:\Python27\lib\site-packages\django\conf\__init__.py", line
>> 42, in _set
>> up
>>     self._wrapped = Settings(settings_module)
>>   File "C:\Python27\lib\site-packages\django\conf\__init__.py", line
>> 90, in __in
>> it__
>>     mod = importlib.import_module(self.SETTINGS_MODULE)
>>   File "C:\Python27\lib\site-packages\django\utils\importlib.py", line
>> 35, in im
>> port_module
>>     __import__(name)
>>   File "C:\Python27\Lib\site-packages\django\bin\mysite\mysite
>> \settings.py", lin
>> e 15
>>     'NAME': 'C:/Users/songbird/Desktop/bdd/' # Or path to database
>> file if using sqlite3.
>>           ^
>> SyntaxError: invalid syntax
>>
>> I'm on windows 7.
>> So, my question is simple: what am I doing wrong?
>> Thanks a lot in advance.
>> Best
>> -Nicolas-
>>
>
> --
> 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: Django app, part 1, error with the database

Hi Nicolas,

It looks like you have a syntax error in your settings file. It's
possible you left of a comma after one of your dictionary values, or
didn't close a brace. I can't tell for sure without more context: could
you please provide the complete "DATABASES" section of your settings file?

On 11/1/2011 9:46 AM, Nicolas wrote:
> Dear all,
> I'm completly new to the world of django and python.
> I'm trying to go through the tuto "Writing your first Django app, part
> 1" and of course I have an error.
> This error happens in the step "database setup". I edited the settings
> file, wrote 'django.db.backends.sqlite3' for engine and a path to a db
> file (as it's said in the tuto that "If the file doesn't exist, it
> will automatically be created when you synchronize the database for
> the first time" I just set a path to an empty folder).
> And then, I run manage.py syncdb and I get this :
>
> C:\Python27\Lib\site-packages\django\bin\mysite>manage.py syncdb
> Traceback (most recent call last):
> File "C:\Python27\Lib\site-packages\django\bin\mysite\manage.py",
> line 9, in <
> module>
> execute_from_command_line(sys.argv)
> File "C:\Python27\lib\site-packages\django\core\management
> \__init__.py", line
> 422, in execute_from_command_line
> utility.execute()
> File "C:\Python27\lib\site-packages\django\core\management
> \__init__.py", line
> 361, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv)
> File "C:\Python27\lib\site-packages\django\core\management
> \__init__.py", line
> 234, in fetch_command
> app_name = get_commands()[subcommand]
> File "C:\Python27\lib\site-packages\django\core\management
> \__init__.py", line
> 102, in get_commands
> apps = settings.INSTALLED_APPS
> File "C:\Python27\lib\site-packages\django\utils\functional.py",
> line 185, in
> inner
> self._setup()
> File "C:\Python27\lib\site-packages\django\conf\__init__.py", line
> 42, in _set
> up
> self._wrapped = Settings(settings_module)
> File "C:\Python27\lib\site-packages\django\conf\__init__.py", line
> 90, in __in
> it__
> mod = importlib.import_module(self.SETTINGS_MODULE)
> File "C:\Python27\lib\site-packages\django\utils\importlib.py", line
> 35, in im
> port_module
> __import__(name)
> File "C:\Python27\Lib\site-packages\django\bin\mysite\mysite
> \settings.py", lin
> e 15
> 'NAME': 'C:/Users/songbird/Desktop/bdd/' # Or path to database
> file if using sqlite3.
> ^
> SyntaxError: invalid syntax
>
> I'm on windows 7.
> So, my question is simple: what am I doing wrong?
> Thanks a lot in advance.
> Best
> -Nicolas-
>

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

Django app, part 1, error with the database

Dear all,
I'm completly new to the world of django and python.
I'm trying to go through the tuto "Writing your first Django app, part
1" and of course I have an error.
This error happens in the step "database setup". I edited the settings
file, wrote 'django.db.backends.sqlite3' for engine and a path to a db
file (as it's said in the tuto that "If the file doesn't exist, it
will automatically be created when you synchronize the database for
the first time" I just set a path to an empty folder).
And then, I run manage.py syncdb and I get this :

C:\Python27\Lib\site-packages\django\bin\mysite>manage.py syncdb
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\django\bin\mysite\manage.py",
line 9, in <
module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management
\__init__.py", line
422, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management
\__init__.py", line
361, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management
\__init__.py", line
234, in fetch_command
app_name = get_commands()[subcommand]
File "C:\Python27\lib\site-packages\django\core\management
\__init__.py", line
102, in get_commands
apps = settings.INSTALLED_APPS
File "C:\Python27\lib\site-packages\django\utils\functional.py",
line 185, in
inner
self._setup()
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line
42, in _set
up
self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line
90, in __in
it__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line
35, in im
port_module
__import__(name)
File "C:\Python27\Lib\site-packages\django\bin\mysite\mysite
\settings.py", lin
e 15
'NAME': 'C:/Users/songbird/Desktop/bdd/' # Or path to database
file if using sqlite3.
^
SyntaxError: invalid syntax

I'm on windows 7.
So, my question is simple: what am I doing wrong?
Thanks a lot in advance.
Best
-Nicolas-

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

django model forms validation and error display

i have this template: http://dpaste.com/645468/ that send data to this
view: http://dpaste.com/645469/. the template display a form made by
modelforms, with a custom layout. i have to validate the fields value
send to view, and then redirect to the same template if the field
validation fails. i've tried to do it by creating an instance of the
related model, then gathering all fields one by one (checking before
if they are present in request.POST) and assigning it to the model
field, and then assigning that model instance to a new modelform
instance. then i've prefromed a .is_valid() check, but it fails since
it require the model instance pc, so that model instance need to exist
on db. so how can i validate those data, and if validation fails,
redirect to another template and display the errors?

--
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: django request.POST data caching

thanks kurtis for the reply.
Anyway: solved -> fixed many error in the view (a lot of lines were
written by copy and paste....)

On 30 Ott, 08:48, Kurtis Mullins <kurtis.mull...@gmail.com> wrote:
> I apologize if this answer doesn't help much -- I'm confused by your
> question.
>
> request.POST doesn't cache anything. It's simply filled with data submitted
> by the browser on that specific request.
>
> If you click "Back" on your browser, many times the browsers will simply
> re-fill those form fields in with what you had typed in before. This is
> nothing specific to Django.
>
> I'm not aware of any HTML methods to inhibit this behavior. Keep in mind
> that it is good practise to have a redirect after every POST. For example,
> when the user submits the form, process that form, then redirect them to
> display whatever you want them to see, even if it is just additional form
> fields (within the same, or another, view).
>
> I'm not sure what you're trying to achieve by clicking on the "back" button
> -- but if you give some more information on what you're actually trying to
> achieve, I'd be happy to try give you some better help.
>
> Good luck!
>
>
>
>
>
>
>
> On Sat, Oct 29, 2011 at 10:06 AM, luke lukes <lordluk...@gmail.com> wrote:
> > hi have a template with a form and many inputs that pass some data
> > trough a POST request to a view, that process them and send the result
> > to another template. in the final template, if i use the browser back
> > button to jump to the first view, i can see again old data. i refresh
> > the page and i insert new data, i submit again but some old data
> > remain when i see the final view. the problem remain even if i restart
> > the debug server. it seems that there's some data-caching that i can
> > solve (and somethimes not) only flushing browser cache. this is the
> > view code:http://dpaste.com/640956/and the first template code:
> >http://dpaste.com/640960/. someone on stackoverflow.com that is cache
> > navigator and suggest me to use a custom middleware to disable it:
> >http://stackoverflow.com/questions/7921457/django-post-request-data-c...
> > ,
> > so i followed the advice. i also added the pragma no cache meta tag in
> > the html head section, but none of these helped. the problem remains.
> > any suggestion?
>
> > --
> > 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.