Re: Dajaxice and CSRF issues

2011-04-17 Thread Red
Hi,

I found this thread today as I have come across the same problem.

I did find a solution that seems to work OK and I'd like to post
it and get some feedback.

I've built a standard user log in pretty much exactly the same
as in the django docs.

I now want to create a 2nd log in method that use Dajax.

> At the point the first AJAX request is sent, the user doesn't have a validCSRF
> token yet, since the user hasn't visited a Django page yet. .That's why a GET
> may be preferable here.

This is the problem. If a user lands on the home page he has
not yet received any csrf token cookie.

In my basic app the csrf token is only issued once the user gets
to the 2nd page which is the login form. The csrf token
is a hidden form field in the login form.

So in effect if the user wants to login from the home page via dajax
which is their landing page the csrf token needs to be issued
on the very 1st page request.

def index(request):
""" A function to render the home page
"""
# Any page that is not login required but that can
# send a dajax request needs to have a csrf cookie sent to it.

get_token(request)

# Return rendered HTML
return render_to_response('index.html', RequestContext(request))
# end def

Now the user is sent the csrf token via cookie with their 1st page
request
they land on (home page) and can now use dajax to login right away.

Dajax uses post so the csrf id is sent in with the headers.

Seems to work OK for me.


-- 
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: Dajaxice and CSRF issues

2011-04-17 Thread Red
Hi,

> At the point the first AJAX request is sent, the user doesn't have a validCSRF
> token yet, since the user hasn't visited a Django page yet. .That's why a GET
> may be preferable here.

You could send the csrf token with the 1st page request via set-
cookie:

from django.middleware.csrf import get_token

def index(request):
""" A function to render the home page
"""

# Any page that is not login required but that can
# send a dajax request to log in needs to have a csrf cookie sent
to it.
get_token(request)

# Return rendered HTML
return render_to_response('index.html', RequestContext(request))
# end def

-- 
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: How to unit test if user has access to page

2011-04-17 Thread Pedro Kroger
Good point. Thank you guys!

Pedro

On Sat, Apr 16, 2011 at 2:15 PM, Martin Brochhaus
 wrote:
> Also beware! Only do a client.post if you really want to test that a user
> submits a form. Usually when a not-logged-in-user wants to go to a secured
> page immediately he will try a get request and just enter the URL. Sometimes
> your view behaves differently on get and on post (most of the times) so you
> probably should test both scenarios.
> Best regards,
> Martin
>
> --
> 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.
>



-- 
http://pedrokroger.net

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



Programmaticly declaring Models and caching in Admin contrib

2011-04-17 Thread Yuka Poppe
Hi List,

Maybe someone can hint me in the right direction with this.

Im using Django backed by a non relational database, declaring my
django models is enough in order to store and retrieve data, so I
devised a design where models itself are declared by instantiating the
metaclasses and using type() based on entries stored in Model and
associated Field entities in said database.

This is all working as expected, across different threads and
instances even, except for the admin-app, I have to restart the
instances in order for changes to the model to become visible there,
even though everywhere else the changes are instantly and consistently
visible.  Is there perhaps another method of caching models being
employed by the admin-site besides the model cache in
db.models.loader? I'm already updating the model cache the moment a
model/field record fires one of the related signals.

Regard, Yuka

-- 
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: satchmo currency

2011-04-17 Thread ug Charlie
You are right. Thank you.
Then I just need to add a button in the page to switch the currency.
However, still have no idea for this. :(

On 4/15/11, Chris Moffitt  wrote:
> First off, you'll probably get better responses on the Satchmo list.
>
> Secondly, changing languages doesn't change currency symbols, which should
> make sense. 1 Euro is not equal to 1 Dollar. If we were to change symbols,
> we'd also need to do a currency conversion. It's not impossible by any means
> just not something that is done out of the box.
>
> -Chris
>
> On Thu, Apr 14, 2011 at 11:31 PM, ug Charlie  wrote:
>
>> Hello, I just make a satchmo shop. 3 languages.
>>
>> But the currency make me stuck.
>>
>> I just want the site change the currency when changing the language.
>>
>> In satchmo settings.py, it is like
>>
>> L10N_SETTINGS = {
>>'currency_formats' : {
>>'EURO' : {
>>'symbol': u'€ ',
>>'positive' : u"€ %(val)0.2f",
>>'negative': u"€ (%(val)0.2f)",
>>'decimal' : ','
>>},
>>'USD' : {
>>'symbol': u'$ ',
>>'positive' : u"$%(val)0.2f",
>>'negative': u"$(%(val)0.2f)",
>>'decimal' : ','
>>},
>>},
>>'default_currency' : 'EURO',
>>'show_admin_translations': True,
>>'allow_translation_choice': True,
>> }
>>
>> I do not really know how to do that..:(
>>
>> --
>> 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.
>>
>>
>
> --
> 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.
>
>

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



how to find if a record changed

2011-04-17 Thread Aref
Hello,

I have a text field which could be updated regularly and I want to
automatically attach a date every time the record is updated--but only
if the record is updated. Can this be done in django and how would I
go about doing it.
Thanks.

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



How to send email from html?

2011-04-17 Thread LIU Liang
I want to realize the e-mail verification for the social network.
But not clear about the code below:
what does the '$' and the '#' mean? and the method 'submit()?

{% trans "re-send
verification e-mail" %}

I want that after clicking the button 'Verify', it should send out an
e-mail. However, I have copied the apps account and emailverfication
to my project. but it doesn't work. I need help please.

-- 
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: How to send email from html?

2011-04-17 Thread Mengu
that is actually jquery code, mixed with a django template variable.

$ is an alias for jquery which lets you do some nice things with it.
you can also replace it with "jQuery('#send_..')".

#send_{{ forloop.counter }} means the element with id "send_x". x
there is whatever the current forloop.counter is.

so the whole code "$('#send_{{ forloop.counter }}').submit();" means
"submit the dom element which has the id of 'send_x'"

On Apr 17, 6:47 pm, LIU Liang  wrote:
> I want to realize the e-mail verification for the social network.
> But not clear about the code below:
> what does the '$' and the '#' mean? and the method 'submit()?
>
>     {% trans "re-send
> verification e-mail" %}
>
> I want that after clicking the button 'Verify', it should send out an
> e-mail. However, I have copied the apps account and emailverfication
> to my project. but it doesn't work. I need help please.

-- 
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: how to find if a record changed

2011-04-17 Thread Mengu
i believe it does. you can override the "save" method of your model
like this:

from django.db import models
from datetime import datetime

# Create your models here.
class TestModel(models.Model):
first_field = models.CharField(max_length=255)
second_field = models.BooleanField()
updated_at = models.DateTimeField(default=datetime.now)

def save(self, *args, **kwargs):
if hasattr(self, 'id') and getattr(self, 'id') is not None:
self.updated_at = datetime.now()
super(TestModel, self).save(*args, **kwargs)

On Apr 17, 6:25 pm, Aref  wrote:
> Hello,
>
> I have a text field which could be updated regularly and I want to
> automatically attach a date every time the record is updated--but only
> if the record is updated. Can this be done in django and how would I
> go about doing it.
> Thanks.

-- 
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: best practice override admin 1.2 "delete"?

2011-04-17 Thread Mengu
while i was reading the docs, i've noticed something and felt like i
should warn you. my suggestion is not the best practice if you are
doing something like
"Model.objects.filter(some_field=some_value).delete()". for deletions
like that you should use pre_delete signal [1].

[1] 
http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.pre_delete

good luck.

On Apr 14, 3:21 pm, Mengu  wrote:
> you can override the delete method for your models and set a column
> ie, is_deleted to True or something else in there. so, subclass
> models.Model, override delete option and your models should extend
> your new subclass.
>
> On Apr 14, 11:51 am, ëq  wrote:
>
>
>
>
>
>
>
> > Hi list,
>
> > We have a production django app using the default admin view, but some of
> > the super users delete a record in a model and affect other related data,
> > this cause inconsistency and corruption. What's the best practice to
> > override django admin's default delete behavior and implement some kind of
> > "Recycle Bin" for models without harassing much the existing code?
>
> > Any idea is appreciated. 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-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: how to find if a record changed

2011-04-17 Thread Aref
Thank you Mengu. I have tried what you suggested (I was concatenating
the date to the updated field instead) but the problem is that every
time there is a save the date gets added even though the field has not
changed. So I wanted to test if the field was changed and if so
concatenate the date to it if not I would do nothing.
Thanks for taking the time to respond to my question. Much
appreciated.

On Apr 17, 11:01 am, Mengu  wrote:
> i believe it does. you can override the "save" method of your model
> like this:
>
> from django.db import models
> from datetime import datetime
>
> # Create your models here.
> class TestModel(models.Model):
>         first_field = models.CharField(max_length=255)
>         second_field = models.BooleanField()
>         updated_at = models.DateTimeField(default=datetime.now)
>
>         def save(self, *args, **kwargs):
>                 if hasattr(self, 'id') and getattr(self, 'id') is not None:
>                         self.updated_at = datetime.now()
>                 super(TestModel, self).save(*args, **kwargs)
>
> On Apr 17, 6:25 pm, Aref  wrote:
>
> > Hello,
>
> > I have a text field which could be updated regularly and I want to
> > automatically attach a date every time the record is updated--but only
> > if the record is updated. Can this be done in django and how would I
> > go about doing it.
> > Thanks.

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



Default from display for charfield

2011-04-17 Thread Richard Smith
Hello all:

I am new to Django, and have what I hope is a simple question.
 in my model:
class DESubscription(models.Model):
SubscriptionIssuedGUID =
models.CharField(max_length=36,unique=True,default=make_model_uuid)

in my view:

class AddSubscriptionForm(ModelForm):
class Meta:
model = DESubscription
fields = ('SubscriptionIssuedGUID')

In my template:


Build new subscripton {{ strExtraPrompt }}
{% csrf_token
%}
{% for field in form %}
{{ field.label_tag}}: {{ field }}
{% if field.errors %}
{{ field.errors }}
{% endif %}
{% endfor %}






My question:

How do i get the browser to display a field large enough for 36 chars.
I get a text area that is about 1/2 the size of the input/

Thanks for any assistance.

Richard

-- 
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: how to find if a record changed

2011-04-17 Thread Joel Goldstick
On Sun, Apr 17, 2011 at 1:25 PM, Aref  wrote:

> Thank you Mengu. I have tried what you suggested (I was concatenating
> the date to the updated field instead) but the problem is that every
> time there is a save the date gets added even though the field has not
> changed. So I wanted to test if the field was changed and if so
> concatenate the date to it if not I would do nothing.
> Thanks for taking the time to respond to my question. Much
> appreciated.
>
> On Apr 17, 11:01 am, Mengu  wrote:
> > i believe it does. you can override the "save" method of your model
> > like this:
> >
> > from django.db import models
> > from datetime import datetime
> >
> > # Create your models here.
> > class TestModel(models.Model):
> > first_field = models.CharField(max_length=255)
> > second_field = models.BooleanField()
> > updated_at = models.DateTimeField(default=datetime.now)
> >
> > def save(self, *args, **kwargs):
> > if hasattr(self, 'id') and getattr(self, 'id') is not
> None:
> > self.updated_at = datetime.now()
> > super(TestModel, self).save(*args, **kwargs)
> >
> > On Apr 17, 6:25 pm, Aref  wrote:
> >
> > > Hello,
> >
> > > I have a text field which could be updated regularly and I want to
> > > automatically attach a date every time the record is updated--but only
> > > if the record is updated. Can this be done in django and how would I
> > > go about doing it.
> > > Thanks.
>
> --
> 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.
>
>
Before you do the save, compare your new data in that field to what is in
the database, and if different, change the timestamp

-- 
Joel Goldstick

-- 
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: how to find if a record changed

2011-04-17 Thread Aref
I managed to solve the issue. Here's the code:

def save(self, *args, **kwargs):
if hasattr(self, 'id') and getattr(self, 'id') is not None:
old_issue = Issues.objects.get(id=getattr(self,'id'))
self.updated_at = datetime.datetime.now()
if old_issue.issue != self.issue:
self.issue = self.issue +' '+':' + 'Modified' + ' ' +
str(datetime.datetime.now())
super(Issues, self).save(*args, **kwargs)
the table is Issues and has a field called issue. Having a record open
I get its id and then a copy of the record in the database. I compare
to the record as it is in the admin screen. If they are the same I do
not append the date (there is certainly a more elegant way of
appending the date than what I have here) and if not no date is
appended. This works as I intend.



On Apr 17, 11:25 am, Aref  wrote:
> Thank you Mengu. I have tried what you suggested (I was concatenating
> the date to the updated field instead) but the problem is that every
> time there is a save the date gets added even though the field has not
> changed. So I wanted to test if the field was changed and if so
> concatenate the date to it if not I would do nothing.
> Thanks for taking the time to respond to my question. Much
> appreciated.
>
> On Apr 17, 11:01 am, Mengu  wrote:
>
> > i believe it does. you can override the "save" method of your model
> > like this:
>
> > from django.db import models
> > from datetime import datetime
>
> > # Create your models here.
> > class TestModel(models.Model):
> >         first_field = models.CharField(max_length=255)
> >         second_field = models.BooleanField()
> >         updated_at = models.DateTimeField(default=datetime.now)
>
> >         def save(self, *args, **kwargs):
> >                 if hasattr(self, 'id') and getattr(self, 'id') is not None:
> >                         self.updated_at = datetime.now()
> >                 super(TestModel, self).save(*args, **kwargs)
>
> > On Apr 17, 6:25 pm, Aref  wrote:
>
> > > Hello,
>
> > > I have a text field which could be updated regularly and I want to
> > > automatically attach a date every time the record is updated--but only
> > > if the record is updated. Can this be done in django and how would I
> > > go about doing it.
> > > Thanks.

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



Force lowercase in Django-Taggit

2011-04-17 Thread shacker
I've just finished migrating my site's tagging system from django-
tagging to django-taggit. Wrote up some notes on the migration process
in case useful to anyone else:

http://birdhouse.org/blog/2011/04/17/migrate-django-tagging-taggit/

But I still have two questions outstanding:

1) Unlike django-tagging, taggit does not have a global option to
force lowercase tags. This means users can create both tags "Music"
and "music" and both go into the system as separate taxonomies (one
with slug music_1), which is confusing for users. I can't even think
of a use case for this, and would expect reasonable defaults to try
and limit tag redundancy, but ah well.

2) The tag cloud display provided by the separate taggit-template-tags
app doesn't provide a way to only display tags in a cloud that have
been used some minimum number of times. The only solution I could come
up with was to check for num_times during the iteration:

{% if tag.num_times > 5 %}
...
{% endif %}

This is horribly inefficient and I've papered over it with caching for
now, but would really like to come up with a better way.

Has anyone encountered/dealt with either of these issues? Ideally
without hacking the apps themselves, but I'll do what needs doing.

Thanks.

-- 
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: Using composition in Django

2011-04-17 Thread Guevara
Thanks for the replies!
if I leave the class person like this:

class Employee(models.Model):
person = models.OneToOneField(Person)

This is SQL generated:

CREATE TABLE "person_person" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(50) NOT NULL,
)
;
CREATE TABLE "employee_employee" (
"person_id" integer NOT NULL UNIQUE,
"address_id" integer NOT NULL PRIMARY KEY,
)

And when I need to get the employee data, I call Person or Employee?
I can call the Employee who is using the person_id?

Thanks!!



On 16 abr, 20:06, Ian Clelland  wrote:
> On Sat, Apr 16, 2011 at 1:35 PM, Guevara  wrote:
> > Hello!
> > I have two class, Person and employee, i need make a composition for
> > this (Prefer composition over inheritance):
>
> > class Person(models.Model):
> >    name = models.CharField(max_length=50)
> >    date_inclusion = models.DateField()
> >    # others fields
>
> > class Employee(models.Model):
> >    person = models.OneToOneField(Person, primary_key=True)
> >    address = models.OneToOneField(Address, primary_key=True)
>
> > The SQL generate is:
>
> > BEGIN;
> > CREATE TABLE "person_person" (
> >    "id" serial NOT NULL PRIMARY KEY,
> >    "name" varchar(50) NOT NULL,
> >    "date_inclusion" date NOT NULL,
> > )
> > ;
> > CREATE TABLE "employee_employee" (
> >    "person_id" integer NOT NULL PRIMARY KEY,
> >    "address_id" integer NOT NULL PRIMARY KEY,
> > )
> > ;
>
> > This is correct? Should generate the id of the employee?
>
> I don't think it's correct -- a database table shouldn't have two distinct
> primary keys. It's the "primary_key=True" part of your Employee model fields
> that is doing this, and is also stopping an "id" field from being
> automatically generated.
>
> If you write Employee like this:
>
> class Employee(models.Model):
>    person = models.OneToOneField(Person)
>    address = models.OneToOneField(Address)
>
> Then it will generate SQL like this:
>
> CREATE TABLE "employee_employee" (
>    "id" serial NOT NULL PRIMARY KEY,
>    "person_id" integer NOT NULL,
>    "address_id" integer NOT NULL
> )
> ;
>
> which is probably closer to what you're expecting.
>
> --
> Regards,
> Ian Clelland
> 

-- 
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: Using composition in Django

2011-04-17 Thread W Craig Trader

On 04/16/2011 04:35 PM, Guevara wrote:

Hello!
I have two class, Person and employee, i need make a composition for
this (Prefer composition over inheritance):

class Person(models.Model):
 name = models.CharField(max_length=50)
 date_inclusion = models.DateField()
 # others fields

class Employee(models.Model):
 person = models.OneToOneField(Person, primary_key=True)
 address = models.OneToOneField(Address, primary_key=True)


The SQL generate is:


BEGIN;
CREATE TABLE "person_person" (
 "id" serial NOT NULL PRIMARY KEY,
 "name" varchar(50) NOT NULL,
 "date_inclusion" date NOT NULL,
)
;
CREATE TABLE "employee_employee" (
 "person_id" integer NOT NULL PRIMARY KEY,
 "address_id" integer NOT NULL PRIMARY KEY,
)
;


This is correct? Should generate the id of the employee?
Proxy models could be used for this case?

Thanks!



It's correct in that Django has done exactly what you've told it you want, but I doubt that what 
you've told it is what you REALLY want.


If your goal is to have an employee object that has direct access to all of its related person 
object, and whose only new data field is a reference to an address object, then you should change 
the Employee model to this:


class Address(models.Model):
pass

class Person(models.Model):
name = models.CharField(max_length=50)
date_inclusion = models.DateField()

class Employee(models.Model):
person = models.OneToOneField(Person)
address = models.ForeignKey(Address)


This will change the generated employee table to something like this (for 
SQLite3):

CREATE TABLE "foo_address" (
"id" integer NOT NULL PRIMARY KEY
)
;
CREATE TABLE "foo_person" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(50) NOT NULL,
"date_inclusion" date NOT NULL
)
;
CREATE TABLE "foo_employee" (
"id" integer NOT NULL PRIMARY KEY,
"person_id" integer NOT NULL UNIQUE REFERENCES "foo_person" ("id"),
"address_id" integer NOT NULL REFERENCES "foo_address" ("id")
)
;
CREATE INDEX "foo_employee_b213c1e9" ON "foo_employee" ("address_id");


With these models, every Employee object will have an equivalent Person object 
(though you may have Persons without corresponding Employees).  You can then 
use these models as follows:

(InteractiveConsole)

 from foo.models import *
 from datetime import datetime
 now = datetime.now()
 p = Person( name='Tom', date_inclusion=now )
 p.save()
 a = Address()
 a.save()
 e = Employee( person=p, address=a )
 e.save()
 e.person.name

'Tom'

- Craig -

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



problems with errors in templates

2011-04-17 Thread Antonio Sánchez
hi, im working with a modelform, and overrided clen methos for making
some custom checks, raising a forms.ValidationError when i need, im
sure this error is raised, but i dont know why in the template errors
are not showed (instead of form is showed again cause it's not
valid!), here is some of the code: http://pastebin.com/j2SvJYgA  some
help, thanks

-- 
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: problems with errors in templates

2011-04-17 Thread Daniel Roseman
On Sunday, 17 April 2011 21:41:15 UTC+1, Antonio Sánchez wrote:
>
> hi, im working with a modelform, and overrided clen methos for making 
> some custom checks, raising a forms.ValidationError when i need, im 
> sure this error is raised, but i dont know why in the template errors 
> are not showed (instead of form is showed again cause it's not 
> valid!), here is some of the code: http://pastebin.com/j2SvJYgA  some 
> help, thanks


With that code, if the form is not valid and `new` is None, the form will be 
re-instantiated without any bound data. Replace `if new is None` with `elif 
new is None`. 
--
DR.

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



p.save not working in tutorial

2011-04-17 Thread xpdx
Hi all!

A newbee here.

A little problem going through the tutorial. Hoping somebody can
help.

OS X 10.6.6 with included python version 2.6.1
Downloaded and installed Django from http://www.djangoproject.com/download/
verison 3.1

started tutorial- everything including creation of tables seemed to go
fine.

get to the point where is asks me to

>>> p

>>> p.save
>
>>> Poll.objects.all()
[]

Nothing.

If I do:
>python manage.py dbshell

sqlite> .tables
auth_group  auth_user_user_permissions
auth_group_permissions  django_content_type
auth_messagedjango_session
auth_permission django_site
auth_user   polls_choice
auth_user_groupspolls_poll

I can query the tables and return rows on some of them(including
auth_user), but not the polls_poll or polls_choice. They are empty.

What am I doing wrong? Authentication? I'm clueless

-xpdx

-- 
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: p.save not working in tutorial

2011-04-17 Thread Karen Tracey
On Sun, Apr 17, 2011 at 5:06 PM, xpdx  wrote:

>
> >>> p
> 
> >>> p.save
> >
> >>> Poll.objects.all()
> []


p.save just shows you that p has an attribute named save, and what it is.
You need parentheses after the save to actually call the method:

>>> p.save()

Karen
-- 
http://tracey.org/kmt/

-- 
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: Using composition in Django

2011-04-17 Thread Guevara
Hello Craig!
Thanks for the reply!
Actually I want to use a form that will save the Employee data and
would be sent to the Employee class, and this class has relationship
with the Person and Address, the data would automatically be
persisted.
Follow the instructions you suggested and they match what the manual
says:
http://docs.djangoproject.com/en/dev/topics/db/models/#relationships

I did classes like this:

class Employee (models.Model):
person = models.OneToOneField(Person) # only one person related
address = models.OneToOneField(Address) # only one address for
employee
sectors= models.ForeignKey(Sectors) # one or various sectors for
the employee

This is the SQL:

CREATE TABLE "employee_employee" (
"id" serial NOT NULL PRIMARY KEY,
"person_id" integer NOT NULL UNIQUE,
"address_id" integer NOT NULL UNIQUE,
"sectors_id" integer NOT NULL
)

Thanks for help!




On 17 abr, 17:21, W Craig Trader  wrote:
> On 04/16/2011 04:35 PM, Guevara wrote:
>
>
>
>
>
>
>
>
>
> > Hello!
> > I have two class, Person and employee, i need make a composition for
> > this (Prefer composition over inheritance):
>
> > class Person(models.Model):
> >      name = models.CharField(max_length=50)
> >      date_inclusion = models.DateField()
> >      # others fields
>
> > class Employee(models.Model):
> >      person = models.OneToOneField(Person, primary_key=True)
> >      address = models.OneToOneField(Address, primary_key=True)
>
> > The SQL generate is:
>
> > BEGIN;
> > CREATE TABLE "person_person" (
> >      "id" serial NOT NULL PRIMARY KEY,
> >      "name" varchar(50) NOT NULL,
> >      "date_inclusion" date NOT NULL,
> > )
> > ;
> > CREATE TABLE "employee_employee" (
> >      "person_id" integer NOT NULL PRIMARY KEY,
> >      "address_id" integer NOT NULL PRIMARY KEY,
> > )
> > ;
>
> > This is correct? Should generate the id of the employee?
> > Proxy models could be used for this case?
>
> > Thanks!
>
> It's correct in that Django has done exactly what you've told it you want, 
> but I doubt that what
> you've told it is what you REALLY want.
>
> If your goal is to have an employee object that has direct access to all of 
> its related person
> object, and whose only new data field is a reference to an address object, 
> then you should change
> the Employee model to this:
>
> class Address(models.Model):
>      pass
>
> class Person(models.Model):
>      name = models.CharField(max_length=50)
>      date_inclusion = models.DateField()
>
> class Employee(models.Model):
>      person = models.OneToOneField(Person)
>      address = models.ForeignKey(Address)
>
> This will change the generated employee table to something like this (for 
> SQLite3):
>
> CREATE TABLE "foo_address" (
>      "id" integer NOT NULL PRIMARY KEY
> )
> ;
> CREATE TABLE "foo_person" (
>      "id" integer NOT NULL PRIMARY KEY,
>      "name" varchar(50) NOT NULL,
>      "date_inclusion" date NOT NULL
> )
> ;
> CREATE TABLE "foo_employee" (
>      "id" integer NOT NULL PRIMARY KEY,
>      "person_id" integer NOT NULL UNIQUE REFERENCES "foo_person" ("id"),
>      "address_id" integer NOT NULL REFERENCES "foo_address" ("id")
> )
> ;
> CREATE INDEX "foo_employee_b213c1e9" ON "foo_employee" ("address_id");
>
> With these models, every Employee object will have an equivalent Person 
> object (though you may have Persons without corresponding Employees).  You 
> can then use these models as follows:
>
> (InteractiveConsole)>>>  from foo.models import *
> >>>  from datetime import datetime
> >>>  now = datetime.now()
> >>>  p = Person( name='Tom', date_inclusion=now )
> >>>  p.save()
> >>>  a = Address()
> >>>  a.save()
> >>>  e = Employee( person=p, address=a )
> >>>  e.save()
> >>>  e.person.name
>
> 'Tom'
>
> - Craig -

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



Models within Imported TestCases not created in Test Database...

2011-04-17 Thread Bob Carr
Hi All,

Why don't models within tests imported into app/tests.py get picked up
during test database creation.

from django.db import models
from django.test import TestCase

# This test defined in app/tests.py works fine...
class test_TestModel(TestCase):

  class TestModel(models.Model):
name = models.CharField(max_length=200)

  def test___init__(self):
test = self.TestModel(name='Test')
test.save()
self.failUnless(test and test.pk)

# Importing a test that is equivalent to the test above into app/
tests.py...
# DatabaseError: relation "testimported_testmodel" does not exist
# LINE 1: INSERT INTO "testimported_testmodel" ("name") VALUES
(E'Test...
from testimported.tests import test_TestImportedModel


The only ticked I can find that is related to this issue is:
http://code.djangoproject.com/ticket/7835 but there is no discussion
of imported tests.

Thanks!!!

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