Problem with models

2011-01-04 Thread Quetzacotl
I have model of text, like that:

class Text(models.Model):
 text = models.CharField()
 user = models.ForeignKey(User) #author

And i need to compare two of texts, so i need table like this:

class Compare(models.Model):
 text1 = models.ForeignKey(Text)
 text2 = models.ForeignKey(Text)
 user = models.ForeignKey(User) #who compared

Now i want to get from Text table two random texts and at least one of
them cant be compared yet by given user, how can i do this?

if I try reverse relation compares_set, it doesnt work cause' i have
two foreignkey of same model.
I cant give manaytomanyfield in Text through Compare because i cant
make symmetrical reference with intermediate table.

-- 
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: Problem with models

2011-01-04 Thread Quetzacotl
I have added related_name to text1 and text2, so at least syncdb
worked, but how can I now make this query.

In manager:

self.compare_set.filter(~Q(Q(compare__user=request.user)&Q(Q(compare__text1__id=id)|
Q(compare__text2__id=id.all()

this doesn't work

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



problem with models and foreign keys

2008-07-01 Thread KevinTran

Hi I'm currently reading Learning Website Development with Django and
I'm having a problem with models.  Here is my models.py:

from django.db import models
from django.contrib.auth.models import User


class Link(models.Model):
url = models.URLField(unique=True)

class Bookmark(models.Model):
title = models.CharField(maxlength=200)
link = models.ForeignKey(Link)
user = models.ForeignKey(User)

The book says that if I run "python manage.py syncdb" and then "python
manage.py sql bookmarks" then I should get the following:

BEGIN;
CREATE TABLE "bookmarks_bookmark" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"user_id" integer NOT NULL REFERENCES
  "auth_user" ("id"),
"link_id" integer NOT NULL REFERENCES
  "bookmarks_link" ("id"),
);
CREATE TABLE "bookmarks_link" (
"id" integer NOT NULL PRIMARY KEY,
"url" varchar(200) NOT NULL UNIQUE
);
COMMIT;

What I actually get is:

BEGIN;
CREATE TABLE "bookmarks_bookmark" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"link_id" integer NOT NULL,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id")
);
CREATE TABLE "bookmarks_link" (
"id" integer NOT NULL PRIMARY KEY,
"url" varchar(200) NOT NULL UNIQUE
);
COMMIT;


Why does the link_id not get referenced as a foreign key the way the
user_id does?  I have a feeling that this is very simple, but I can't
get my head around it.  Thank you.

--~--~-~--~~~---~--~~
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: problem with models and foreign keys

2008-07-01 Thread [EMAIL PROTECTED]

Because the link table is created after the Bookmarks one that would
be a reference to a non-existant table, if you use the sqlall command
instead you will see that the tables are altered to add the
constraint.

On Jul 1, 1:57 am, KevinTran <[EMAIL PROTECTED]> wrote:
> Hi I'm currently reading Learning Website Development with Django and
> I'm having a problem with models.  Here is my models.py:
>
> from django.db import models
> from django.contrib.auth.models import User
>
> class Link(models.Model):
>     url = models.URLField(unique=True)
>
> class Bookmark(models.Model):
>     title = models.CharField(maxlength=200)
>     link = models.ForeignKey(Link)
>     user = models.ForeignKey(User)
>
> The book says that if I run "python manage.py syncdb" and then "python
> manage.py sql bookmarks" then I should get the following:
>
> BEGIN;
> CREATE TABLE "bookmarks_bookmark" (
>     "id" integer NOT NULL PRIMARY KEY,
>     "title" varchar(200) NOT NULL,
>     "user_id" integer NOT NULL REFERENCES
>       "auth_user" ("id"),
>     "link_id" integer NOT NULL REFERENCES
>       "bookmarks_link" ("id"),
> );
> CREATE TABLE "bookmarks_link" (
>     "id" integer NOT NULL PRIMARY KEY,
>     "url" varchar(200) NOT NULL UNIQUE
> );
> COMMIT;
>
> What I actually get is:
>
> BEGIN;
> CREATE TABLE "bookmarks_bookmark" (
>     "id" integer NOT NULL PRIMARY KEY,
>     "title" varchar(200) NOT NULL,
>     "link_id" integer NOT NULL,
>     "user_id" integer NOT NULL REFERENCES "auth_user" ("id")
> );
> CREATE TABLE "bookmarks_link" (
>     "id" integer NOT NULL PRIMARY KEY,
>     "url" varchar(200) NOT NULL UNIQUE
> );
> COMMIT;
>
> Why does the link_id not get referenced as a foreign key the way the
> user_id does?  I have a feeling that this is very simple, but I can't
> get my head around it.  Thank you.
--~--~-~--~~~---~--~~
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: problem with models and foreign keys

2008-07-01 Thread KevinTran

So that means that the tables are correct right?  Is there a way to
control the order of the table creation?

On Jul 1, 12:05 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Because the link table is created after the Bookmarks one that would
> be a reference to a non-existant table, if you use the sqlall command
> instead you will see that the tables are altered to add the
> constraint.
>
> On Jul 1, 1:57 am, KevinTran <[EMAIL PROTECTED]> wrote:
>
> > Hi I'm currently reading Learning Website Development with Django and
> > I'm having a problem with models.  Here is my models.py:
>
> > from django.db import models
> > from django.contrib.auth.models import User
>
> > class Link(models.Model):
> >     url = models.URLField(unique=True)
>
> > class Bookmark(models.Model):
> >     title = models.CharField(maxlength=200)
> >     link = models.ForeignKey(Link)
> >     user = models.ForeignKey(User)
>
> > The book says that if I run "python manage.py syncdb" and then "python
> > manage.py sql bookmarks" then I should get the following:
>
> > BEGIN;
> > CREATE TABLE "bookmarks_bookmark" (
> >     "id" integer NOT NULL PRIMARY KEY,
> >     "title" varchar(200) NOT NULL,
> >     "user_id" integer NOT NULL REFERENCES
> >       "auth_user" ("id"),
> >     "link_id" integer NOT NULL REFERENCES
> >       "bookmarks_link" ("id"),
> > );
> > CREATE TABLE "bookmarks_link" (
> >     "id" integer NOT NULL PRIMARY KEY,
> >     "url" varchar(200) NOT NULL UNIQUE
> > );
> > COMMIT;
>
> > What I actually get is:
>
> > BEGIN;
> > CREATE TABLE "bookmarks_bookmark" (
> >     "id" integer NOT NULL PRIMARY KEY,
> >     "title" varchar(200) NOT NULL,
> >     "link_id" integer NOT NULL,
> >     "user_id" integer NOT NULL REFERENCES "auth_user" ("id")
> > );
> > CREATE TABLE "bookmarks_link" (
> >     "id" integer NOT NULL PRIMARY KEY,
> >     "url" varchar(200) NOT NULL UNIQUE
> > );
> > COMMIT;
>
> > Why does the link_id not get referenced as a foreign key the way the
> > user_id does?  I have a feeling that this is very simple, but I can't
> > get my head around it.  Thank you.
--~--~-~--~~~---~--~~
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: problem with models and foreign keys

2008-07-01 Thread Malcolm Tredinnick


On Tue, 2008-07-01 at 00:09 -0700, KevinTran wrote:
> So that means that the tables are correct right? 

Yes, they're fine. It's just a subtle difference between what "manage.py
sql ..." and "manage.py sqlall ..." prints out. When you run "manage.py
syncdb", it will use the "sqlall" equivalent.

>  Is there a way to
> control the order of the table creation?

No. You don't need to. Django makes sure that the right "ALTER TABLE..."
commands are run later to add cross-references that are needed.

Regards,
Malcolm



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



problem with models while using User's ID as fk to another table

2008-09-07 Thread Juan Hernandez
Hey there, I have this model:

from django.db import models
from django.contrib.auth.models import User
import datetime

class Section(models.Model):
author = models.ForeignKey(User)
name = models.CharField(maxlength=30)
# just the date
creation_date = models.DateField(default=datetime.date)

It uses a FK to django's User model. It runs smoothly when I type syncdb and
creates the tables as expected.

When I try to add something to that model. lets say this:

# import the stuff I need
from django.contrib.auth.models import User
from blog.models import Section as s

# Create the QuerySet necessary for the model's FK
x = User.objects.get(id=1)

# create the QuerySet
s(author=x, name='This is a Name')

I get this error:

/usr/lib/python2.5/site-packages/django/db/models/base.py in __init__(self,
*args, **kwargs)
148 val = kwargs.pop(field.attname,
field.get_default())
149 else:
--> 150 val = field.get_default()
151 setattr(self, field.attname, val)
152

/usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py in
get_default(self)
196 if self.default is not NOT_PROVIDED:
197 if callable(self.default):
--> 198 return self.default()
199 return self.default
200 if not self.empty_strings_allowed or self.null:

: function takes exactly 3 arguments (0 given)

I have tried giving all kind of values without any success. Somehow it's not
seeing the values I'm passing. This is the first time i've used the User
model as a reference to another table, I may be missing something

BTW I'm using 0.96

Any ideas??

Thank you
jhv

--~--~-~--~~~---~--~~
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: problem with models while using User's ID as fk to another table

2008-09-07 Thread Karen Tracey
On Sun, Sep 7, 2008 at 4:43 PM, Juan Hernandez <[EMAIL PROTECTED]>wrote:

> Hey there, I have this model:
>
> from django.db import models
> from django.contrib.auth.models import User
> import datetime
>
> class Section(models.Model):
> author = models.ForeignKey(User)
> name = models.CharField(maxlength=30)
> # just the date
> creation_date = models.DateField(default=datetime.date)
>
> It uses a FK to django's User model. It runs smoothly when I type syncdb
> and creates the tables as expected.
>
> When I try to add something to that model. lets say this:
>
> # import the stuff I need
> from django.contrib.auth.models import User
> from blog.models import Section as s
>
> # Create the QuerySet necessary for the model's FK
> x = User.objects.get(id=1)
>
> # create the QuerySet
> s(author=x, name='This is a Name')
>
> I get this error:
>
> /usr/lib/python2.5/site-packages/django/db/models/base.py in __init__(self,
> *args, **kwargs)
> 148 val = kwargs.pop(field.attname,
> field.get_default())
> 149 else:
> --> 150 val = field.get_default()
> 151 setattr(self, field.attname, val)
> 152
>
> /usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py in
> get_default(self)
> 196 if self.default is not NOT_PROVIDED:
> 197 if callable(self.default):
> --> 198 return self.default()
> 199 return self.default
> 200 if not self.empty_strings_allowed or self.null:
>
> : function takes exactly 3 arguments (0 given)
>
> I have tried giving all kind of values without any success. Somehow it's
> not seeing the values I'm passing. This is the first time i've used the User
> model as a reference to another table, I may be missing something
>
> BTW I'm using 0.96
>
> Any ideas??
>

It's running into trouble trying to call the default you have specified for
creation_date, because datetime.date takes three arguments, not none:

>>> import datetime
>>> datetime.date()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: function takes exactly 3 arguments (0 given)

I think you want to specify datetime.date.today instead:

>>> datetime.date.today()
datetime.date(2008, 9, 7)

Karen

--~--~-~--~~~---~--~~
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: problem with models while using User's ID as fk to another table

2008-09-07 Thread Juan Hernandez
DUH hehehe... thanks Karen

On Mon, Sep 8, 2008 at 4:26 PM, Karen Tracey <[EMAIL PROTECTED]> wrote:

> On Sun, Sep 7, 2008 at 4:43 PM, Juan Hernandez <[EMAIL PROTECTED]>wrote:
>
>> Hey there, I have this model:
>>
>> from django.db import models
>> from django.contrib.auth.models import User
>> import datetime
>>
>> class Section(models.Model):
>> author = models.ForeignKey(User)
>> name = models.CharField(maxlength=30)
>> # just the date
>> creation_date = models.DateField(default=datetime.date)
>>
>> It uses a FK to django's User model. It runs smoothly when I type syncdb
>> and creates the tables as expected.
>>
>> When I try to add something to that model. lets say this:
>>
>> # import the stuff I need
>> from django.contrib.auth.models import User
>> from blog.models import Section as s
>>
>> # Create the QuerySet necessary for the model's FK
>> x = User.objects.get(id=1)
>>
>> # create the QuerySet
>> s(author=x, name='This is a Name')
>>
>> I get this error:
>>
>> /usr/lib/python2.5/site-packages/django/db/models/base.py in
>> __init__(self, *args, **kwargs)
>> 148 val = kwargs.pop(field.attname,
>> field.get_default())
>> 149 else:
>> --> 150 val = field.get_default()
>> 151 setattr(self, field.attname, val)
>> 152
>>
>> /usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py in
>> get_default(self)
>> 196 if self.default is not NOT_PROVIDED:
>> 197 if callable(self.default):
>> --> 198 return self.default()
>> 199 return self.default
>> 200 if not self.empty_strings_allowed or self.null:
>>
>> : function takes exactly 3 arguments (0
>> given)
>>
>> I have tried giving all kind of values without any success. Somehow it's
>> not seeing the values I'm passing. This is the first time i've used the User
>> model as a reference to another table, I may be missing something
>>
>> BTW I'm using 0.96
>>
>> Any ideas??
>>
>
> It's running into trouble trying to call the default you have specified for
> creation_date, because datetime.date takes three arguments, not none:
>
> >>> import datetime
> >>> datetime.date()
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: function takes exactly 3 arguments (0 given)
>
> I think you want to specify datetime.date.today instead:
>
> >>> datetime.date.today()
> datetime.date(2008, 9, 7)
>
> Karen
>
> >
>

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