Re: Using ManytoManyField's in signals

2008-09-03 Thread Rajesh Dhawan


>
> for s in site:
>     ti.site.add(s)
>
> almost worked.  I got an error that the ManyRelatedManager wasn't
> iterable.  So I did a little research and found that
>
> for s in site.all():
>     ti.site.add(s)
>
> that works because the query set that it builds is iterable.

Yes. Sorry, I should've suggested that in my original snippet. The
singular field name keeps giving me a brain freeze. You should
seriously consider renaming that M2M field to sites if you need my
help ;)


--~--~-~--~~~---~--~~
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: Using ManytoManyField's in signals

2008-09-03 Thread nek4life



On Sep 3, 4:08 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> On Sep 3, 3:59 pm, nek4life <[EMAIL PROTECTED]> wrote:
>
> > I'm using Django Beta 2
>
> If, instead of top-posting, you bottom-post your responses, it's
> easier for people to see which questions you're responding to.
>
> 
>
>
>
>
>
> > Here is my article model
>
> > from django.db import models
> > from django.contrib.sites.models import Site
> > from django.contrib.sites.managers import CurrentSiteManager
> > from django.contrib import admin
> > from django.contrib.auth.models import User
>
> > class Article(models.Model):
> >     """Article Model"""
> >     STATUS_CHOICES = (
> >         (1, 'Draft'),
> >         (2, 'Public'),
> >     )
> >     title          = models.CharField(max_length=200)
> >     slug           = models.SlugField()
> >     author         = models.ForeignKey(User, blank=True, null=True)
> >     lead           = models.TextField()
> >     lead_image     = models.FileField(upload_to="lead_images",
> > blank=True)
> >     body           = models.TextField()
> >     site           = models.ManyToManyField(Site)
> >     on_site        = CurrentSiteManager()
> >     status         = models.IntegerField(choices=STATUS_CHOICES,
> > default=1)
> >     allow_comments = models.BooleanField(default=True)
> >     publish        = models.DateTimeField()
> >     created        = models.DateTimeField(auto_now_add=True)
> >     modified       = models.DateTimeField(auto_now=True)
>
> >     def __unicode__(self):
> >         return '%s' % self.title
>
> >     class Meta:
> >         verbose_name        = ('article')
> >         verbose_name_plural = ('articles')
>
> Ah, I previously assumed that Article.site is a ForeignKey. See if you
> had named it 'sites', it would've been easier to guess?
>
> Anyway, change that previous ti.site.add(site) snippet to:
>
> for s in site:
>    ti.site.add(s)
>
> You could also try:
>    ti.site = site
>
> But that will wipe out any previous values of site you may have
> associated with the same ti instance.
>
> -RD

Sorry about top posting.  This is about the 3rd time I've used google
groups before, but what you're saying makes sense.  I'm sure I'll have
many more questions to get the hang of it!

for s in site:
ti.site.add(s)

almost worked.  I got an error that the ManyRelatedManager wasn't
iterable.  So I did a little research and found that

for s in site.all():
ti.site.add(s)

that works because the query set that it builds is iterable.

so that final snippet looked like this.

ti, created = TumbleItem.objects.get_or_create(content_type=ctype,
object_id=instance.id, pub_date=pub_date)
for s in site.all():
ti.site.add(s)

Thanks a ton.  You've been a huge help.

--~--~-~--~~~---~--~~
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: Using ManytoManyField's in signals

2008-09-03 Thread Rajesh Dhawan

On Sep 3, 3:59 pm, nek4life <[EMAIL PROTECTED]> wrote:
> I'm using Django Beta 2

If, instead of top-posting, you bottom-post your responses, it's
easier for people to see which questions you're responding to.


>
> Here is my article model
>
> from django.db import models
> from django.contrib.sites.models import Site
> from django.contrib.sites.managers import CurrentSiteManager
> from django.contrib import admin
> from django.contrib.auth.models import User
>
> class Article(models.Model):
> """Article Model"""
> STATUS_CHOICES = (
> (1, 'Draft'),
> (2, 'Public'),
> )
> title  = models.CharField(max_length=200)
> slug   = models.SlugField()
> author = models.ForeignKey(User, blank=True, null=True)
> lead   = models.TextField()
> lead_image = models.FileField(upload_to="lead_images",
> blank=True)
> body   = models.TextField()
> site   = models.ManyToManyField(Site)
> on_site= CurrentSiteManager()
> status = models.IntegerField(choices=STATUS_CHOICES,
> default=1)
> allow_comments = models.BooleanField(default=True)
> publish= models.DateTimeField()
> created= models.DateTimeField(auto_now_add=True)
> modified   = models.DateTimeField(auto_now=True)
>
> def __unicode__(self):
> return '%s' % self.title
>
> class Meta:
> verbose_name= ('article')
> verbose_name_plural = ('articles')


Ah, I previously assumed that Article.site is a ForeignKey. See if you
had named it 'sites', it would've been easier to guess?

Anyway, change that previous ti.site.add(site) snippet to:

for s in site:
   ti.site.add(s)

You could also try:
   ti.site = site

But that will wipe out any previous values of site you may have
associated with the same ti instance.

-RD

--~--~-~--~~~---~--~~
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: Using ManytoManyField's in signals

2008-09-03 Thread nek4life

I'm using Django Beta 2

This is my post data

bodyu'body'
status  u'2'
allow_comments  u'on'
_save  u'Save'
leadu'lead'
author  u'1'
title   u'Test Post'
siteu'2'
publish_1   u'15:04:22'
publish_0   u'2008-09-03'
lead_image  u''
slugu'test'

Here is my article model

from django.db import models
from django.contrib.sites.models import Site
from django.contrib.sites.managers import CurrentSiteManager
from django.contrib import admin
from django.contrib.auth.models import User


class Article(models.Model):
"""Article Model"""
STATUS_CHOICES = (
(1, 'Draft'),
(2, 'Public'),
)
title  = models.CharField(max_length=200)
slug   = models.SlugField()
author = models.ForeignKey(User, blank=True, null=True)
lead   = models.TextField()
lead_image = models.FileField(upload_to="lead_images",
blank=True)
body   = models.TextField()
site   = models.ManyToManyField(Site)
on_site= CurrentSiteManager()
status = models.IntegerField(choices=STATUS_CHOICES,
default=1)
allow_comments = models.BooleanField(default=True)
publish= models.DateTimeField()
created= models.DateTimeField(auto_now_add=True)
modified   = models.DateTimeField(auto_now=True)

def __unicode__(self):
return '%s' % self.title

class Meta:
verbose_name= ('article')
verbose_name_plural = ('articles')

On Sep 3, 3:31 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> > Seems like I'm headed in the right direction, I made the changes you
> > suggested but now I'm getting a warning.
> > 
>
> > Exception Type: Warning at /admin/posts/article/add/
> > Exception Value: Incorrect integer value:
> > ' > 0xfd8d50>' for column 'site_id' at row 1
>
> > Not sure what's going on...
>
> I don't know how your Article and other such models are defined, by
> are you guaranteed to have a valid Site instance for instance.site or
> can that value be None? If you're in DEBUG mode (settings.DEBUG=True),
> you should be able to see the value of 'site' in your browser. If not,
> try printing 'site' and 'site.pk' just before you make the site.add()
> call.
>
> Also, what version of Django are you on?
>
> -Rajesh D
--~--~-~--~~~---~--~~
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: Using ManytoManyField's in signals

2008-09-03 Thread Rajesh Dhawan

> Seems like I'm headed in the right direction, I made the changes you
> suggested but now I'm getting a warning.

> 
>
> Exception Type: Warning at /admin/posts/article/add/
> Exception Value: Incorrect integer value:
> ' 0xfd8d50>' for column 'site_id' at row 1
>
> Not sure what's going on...

I don't know how your Article and other such models are defined, by
are you guaranteed to have a valid Site instance for instance.site or
can that value be None? If you're in DEBUG mode (settings.DEBUG=True),
you should be able to see the value of 'site' in your browser. If not,
try printing 'site' and 'site.pk' just before you make the site.add()
call.

Also, what version of Django are you on?

-Rajesh D


--~--~-~--~~~---~--~~
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: Using ManytoManyField's in signals

2008-09-03 Thread Rajesh Dhawan

Hi,

> from django.db import models
> from django.contrib.contenttypes import generic
> from django.template.loader import render_to_string
> from django.db.models import signals
> from django.contrib.contenttypes.models import ContentType
> from django.dispatch import dispatcher
> from django.contrib.sites.models import Site
> from django.contrib.sites.managers import CurrentSiteManager
> from publish.posts.models import *
>
> class TumbleItem(models.Model):
> content_type = models.ForeignKey(ContentType)
> object_id= models.PositiveIntegerField()
> pub_date = models.DateTimeField()
> site = models.ManyToManyField(Site)
> objects  = models.Manager()
> on_site  = CurrentSiteManager()
>
> content_object = generic.GenericForeignKey('content_type',
> 'object_id')
>
> def get_rendered_html(self):
> template_name = 'tumblelog/includes/tumble_item_%s.html' %
> (self.content_type.name)
> return render_to_string(template_name, { 'object':
> self.content_object })
>
> def create_tumble_item(sender, instance, signal, *args, **kwargs):
> # Check to see if the object was just created for the first time
> if 'created' in kwargs:
> if kwargs['created']:
> create = True
>
> # Get the instance's content type
> ctype = ContentType.objects.get_for_model(instance)
>
> pub_date = instance.publish
> site = instance.site
>
> if instance.status == 1:
> create = False
>
> if create:
> ti =
> TumbleItem.objects.get_or_create(content_type=ctype,
> object_id=instance.id, pub_date=pub_date, site=site)

Change that as follows:

if create:
ti, created = TumbleItem.objects.get_or_create(content_type=ctype,
object_id=instance.id, pub_date=pub_date)
ti.site.add(site)

Also, it's common practice to pluralize the names of your
ManyToManyFields...so in the above case it would be 'sites' instead of
'site'.

-Rajesh D
--~--~-~--~~~---~--~~
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: Using ManytoManyField's in signals

2008-09-03 Thread nek4life

Thanks Rajesh,

Seems like I'm headed in the right direction, I made the changes you
suggested but now I'm getting a warning.

Environment:

Request Method: POST
Request URL: http://localhost:8000/admin/posts/article/add/
Django Version: 1.0-beta_2-SVN-unknown
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'publish.posts',
 'publish.tumblelog']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware')


Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/core/handlers/base.py" in get_response
  86. response = callback(request, *callback_args,
**callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/contrib/admin/sites.py" in root
  173. return self.model_page(request, *url.split('/',
2))
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  44. response = view_func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/contrib/admin/sites.py" in model_page
  192. return admin_obj(request, rest_of_url)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/contrib/admin/options.py" in __call__
  185. return self.add_view(request)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/db/transaction.py" in _commit_on_success
  238. res = func(*args, **kw)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/contrib/admin/options.py" in add_view
  493. self.save_model(request, new_object, form,
change=False)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/contrib/admin/options.py" in save_model
  367. obj.save()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/db/models/base.py" in save
  280. self.save_base(force_insert=force_insert,
force_update=force_update)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/db/models/base.py" in save_base
  363. created=(not record_exists), raw=raw)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/dispatch/dispatcher.py" in send
  148. response = receiver(signal=self, sender=sender,
**named)
File "/Users/nek4life/djcode/publish/../publish/tumblelog/models.py"
in create_tumble_item
  43. ti.site.add(site)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/db/models/fields/related.py" in add
  374. self._add_items(self.source_col_name,
self.target_col_name, *objs)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/db/models/fields/related.py" in _add_items
  446. [self._pk_val, obj_id])
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/django/db/backends/util.py" in execute
  19. return self.cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/MySQL_python-1.2.2-py2.5-macosx-10.3-i386.egg/MySQLdb/
cursors.py" in execute
  168. if not self._defer_warnings: self._warning_check()
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
site-packages/MySQL_python-1.2.2-py2.5-macosx-10.3-i386.egg/MySQLdb/
cursors.py" in _warning_check
  82. warn(w[-1], self.Warning, 3)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
warnings.py" in warn
  62.   globals)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/
warnings.py" in warn_explicit
  102. raise message

Exception Type: Warning at /admin/posts/article/add/
Exception Value: Incorrect integer value:
'' for column 'site_id' at row 1

Not sure what's going on...

On Sep 3, 2:45 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> Hi,
>
>
>
> > from django.db import models
> > from django.contrib.contenttypes import generic
> > from django.template.loader import render_to_string
> > from django.db.models import signals
> > from django.contrib.contenttypes.models import ContentType
> > from django.dispatch import dispatcher
> > from django.contrib.sites.models import Site
> > from django.contrib.sites.managers import CurrentSiteManager
> > from publish.posts.models import *
>
> > class TumbleItem(models.Model):
> >