using model classes without a server

Visto 2 veces
Saltar al primer mensaje no leído

xamdam

no leída,
19 feb 2006, 13:15:2919/2/06
a Django developers
Django models can already be manipulated outside of the web app with
manage.py shell
I am looking for a way to have a regular python script (say, running a
batch job) use the model classes. It seems like I just have to import
the right stuff (which 'shell' option does automagically), but I
haven't figure out what. Suggestions?

thanks,
max.

Michael Twomey

no leída,
19 feb 2006, 13:36:3619/2/06
a django-d...@googlegroups.com

Hi,

You need to do two things, setup the settings module and then import the models:

# You can also set DJANGO_SETTINGS_MODULE in your shell
import os
os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings"

import django.conf.settings
from django.models.myapp import articles

cheers,
Michael

xamdam

no leída,
19 feb 2006, 13:42:0919/2/06
a Django developers
Nice, thanks!

John Szakmeister

no leída,
20 feb 2006, 6:40:1820/2/06
a django-d...@googlegroups.com

One thing that I've found, at least on Django's trunk code, is that if you do
this, validation doesn't take place when you do model.save(). I tried
setting up a couple of fields as "required if other fields is present", and
it happily saved the model without enforcing it. I've also found that it
doesn't enforce the blank=False attribute either. I'm sure it's something
I'm doing wrong, but just doing things the intuitive way (load the model,
create/modify it, and then saving it) allowed me to insert data that didn't
conform to the restrictions I had placed on the model.

-John

Michael Twomey

no leída,
20 feb 2006, 7:07:4120/2/06
a django-d...@googlegroups.com
On 20/02/06, John Szakmeister <jo...@szakmeister.net> wrote:
> One thing that I've found, at least on Django's trunk code, is that if you do
> this, validation doesn't take place when you do model.save(). I tried
> setting up a couple of fields as "required if other fields is present", and
> it happily saved the model without enforcing it. I've also found that it
> doesn't enforce the blank=False attribute either. I'm sure it's something
> I'm doing wrong, but just doing things the intuitive way (load the model,
> create/modify it, and then saving it) allowed me to insert data that didn't
> conform to the restrictions I had placed on the model.
>

This is true, I've encountered this as well. You do get basic database
level validation of your queries which will catch the big sillies, but
I think the validation happens using the manipulators. Just trying it
now I've gotten some mileage with this:

>>> from django.models.auth import users
>>> manipulator = users.AddManipulator()
>>> manipulator.get_validation_errors(dict(username="mtt",
first_name="Michael", last_name="Twomey", email="mtt"))
{'date_joined_date': ['This field is required.'],
'date_joined_time': ['This field is required.'],
'email': ['Enter a valid e-mail address.'],
'last_login_date': ['This field is required.'],
'last_login_time': ['This field is required.'],
'password': ['This field is required.'],
'username': ['User with this username already exists.']}

What would be nice is some kind of validate flag for save or the class
constructor which raised an exception with this info in it. This would
make life a little easier for scripting.

Another thing missing is logic to construct the slugs, they currently
come from the admin javascript AFAIK.

cheers,
Michael

Adrian Holovaty

no leída,
23 feb 2006, 10:40:5723/2/06
a django-d...@googlegroups.com
On 2/20/06, Michael Twomey <mickt...@gmail.com> wrote:
> This is true, I've encountered this as well. You do get basic database
> level validation of your queries which will catch the big sillies, but
> I think the validation happens using the manipulators. Just trying it
> now I've gotten some mileage with this:
>
> >>> from django.models.auth import users
> >>> manipulator = users.AddManipulator()
> >>> manipulator.get_validation_errors(dict(username="mtt",
> first_name="Michael", last_name="Twomey", email="mtt"))
> {'date_joined_date': ['This field is required.'],
> 'date_joined_time': ['This field is required.'],
> 'email': ['Enter a valid e-mail address.'],
> 'last_login_date': ['This field is required.'],
> 'last_login_time': ['This field is required.'],
> 'password': ['This field is required.'],
> 'username': ['User with this username already exists.']}
>
> What would be nice is some kind of validate flag for save or the class
> constructor which raised an exception with this info in it. This would
> make life a little easier for scripting.

That's exactly what we're discussing in the "validation-aware models" thread. :)

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Michael Twomey

no leída,
25 feb 2006, 9:03:1325/2/06
a django-d...@googlegroups.com
On 23/02/06, Adrian Holovaty <holo...@gmail.com> wrote:
> That's exactly what we're discussing in the "validation-aware models" thread. :)
>

Ack, now I see that thread :)

thanks,
Michael

Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos