Annotate on related field

2016-07-12 Thread Ramashish Baranwal
Hi,

I have the following model definitions:

class Tag(models.Model):
name = models.CharField(max_length=16)

class Blog(models.Model):
name = models.CharField(max_length=16)
tags = models.ManyToManyField(Tag, blank=True)



I want to select blogs with more than one tags. Here is what I am doing 
(for two tags)-

# select blogs for python and java
blogs = Blog.objects.filter(tags__name='python').filter(tags__name='java')



This INNER joins Tag with Blog twice, each time filtering on the given 
name. This works well. Now I want to retrieve the matching tags. If it were 
a single join, I would have done-

blogs = blogs.annotate(tag=F('tags__name'))

Doing this still works, but only retrieves the last tag. How do I retrieve 
the tag name for each join, giving them different names? Something like-

# Below should give tag1 = 'python', tag2 = 'java'
blogs = blogs.annotate(tag1=?, tag2=?)

Thanks,
Ramashish

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/372f27ca-514e-44a0-aeaa-35b8fab67d4e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Tables with same schema

2009-05-01 Thread Ramashish Baranwal

> Plain ol' Python multiple inheritance should work. Just define the
> fields you want in some class:
> {{{
> class PersonMixin(object):
>     first = models.CharField(max_length=16)
>     last = models.CharField(max_length=16)
>
> class Person(models.Model, PersonMixin):
>     pass
>
> class Person2(models.Model, PersonMixin):
>     pass
>
> }}}

Hi George,

I have tried that earlier. Unfortunately, that doesn't work. I think
I'll have to duplicate the definition. :(

Thanks,
Ram


--~--~-~--~~~---~--~~
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: Tables with same schema

2009-04-30 Thread Ramashish Baranwal

>
> > I want to create several tables with same schema. This is to partition
> > the data into different tables. Apart from having model classes with
> > same definition, is there another way to do it?
>
> > Here is a trivial example that duplicates the class definition-
>
> > class Person(models.Model):
> >    first = models.CharField(max_length=16)
> >    last = models.CharField(max_length=16)
>
> > class Person2(models.Model):
> >    first = models.CharField(max_length=16)
> >    last = models.CharField(max_length=16)
>
> > Apart from the code duplication, the above doesn't capture the intent
> > that the two models represent tables with same schema. I'm looking for
> > something like-
>
> > class Person2(Person):
> >    pass
>
> > The above however doesn't work.
> > Any idea?
>
> > Thanks,
> > Ram
>
> I'd do it with one class that has all the common fields that is an abstract
> class(check the docs for this).  And then just 2 classes that subclass it.
> This should do just what you want.
>
Hi Alex,

Thanks for the quick reply. Abstract class fits the need perfectly.
However, I am using Django 0.96 where abstract class functionality is
not available. I can't upgrade  Django currently.
Any other way?

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



Tables with same schema

2009-04-30 Thread Ramashish Baranwal

Hi,

I want to create several tables with same schema. This is to partition
the data into different tables. Apart from having model classes with
same definition, is there another way to do it?

Here is a trivial example that duplicates the class definition-

class Person(models.Model):
first = models.CharField(max_length=16)
last = models.CharField(max_length=16)

class Person2(models.Model):
first = models.CharField(max_length=16)
last = models.CharField(max_length=16)

Apart from the code duplication, the above doesn't capture the intent
that the two models represent tables with same schema. I'm looking for
something like-

class Person2(Person):
pass

The above however doesn't work.
Any idea?

Thanks,
Ram

--~--~-~--~~~---~--~~
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: After sending HttpResponse

2009-03-17 Thread Ramashish Baranwal

> > I need to do some cleanup after sending a response to the client. That
> > means I want to send response without returning a response object.
> > Something like-
>
> > def handler(request):
> >     # ...
> >     resp = HttpResponse(data)
> >     # send response in some way (?)
> >     # clean up, log, etc..
>
> > I know its unusual to do this, but is there a way to achieve it? If
> > not directly, then a way to setup the cleanup code to run after the
> > response has been sent will also do.
>
> What sort of things do you need to do that can't be done before sending
> back the response? Most of the things I can think of (such as the time
> taken to send the response) are out of scope for Django, since it lives
> at a layer below the stuff that actually interacts with the webserver.

Thanks for the reply Malcolm.
My actual requirement is to convert a video file from one format to
another and send the output as a stream, here is an example that uses
ffmpeg to do the conversion. The handler captures the result at stdout
and sends it by wrapping in a FileWrapper.

def handler(request):
# ...
cmd = "ffmpeg -i %s -f flv -" % filename
subp = subprocess.Popen(cmd, stdout=subprocess.PIPE)
result = subp.stdout
wrapper = FileWrapper(result)
response = HttpResponse(wrapper)
# need to wait till the response has actually been sent ..
if subp.wait() != 0:
# something went wrong, log it..

I want to stream as soon as possible without loading the entire result
in memory and preferably without having to save the result to disk. I
have ommitted the code for setting headers, etc. for clarity. Any
ideas or suggestions on how to go here?

Ram

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



After sending HttpResponse

2009-03-17 Thread Ramashish Baranwal

Hi,

I need to do some cleanup after sending a response to the client. That
means I want to send response without returning a response object.
Something like-

def handler(request):
# ...
resp = HttpResponse(data)
# send response in some way (?)
# clean up, log, etc..

I know its unusual to do this, but is there a way to achieve it? If
not directly, then a way to setup the cleanup code to run after the
response has been sent will also do.

Thanks,
Ram

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



Hierarchy on foreign key

2008-10-05 Thread Ramashish Baranwal

Hi,

On admin change list, I want to group objects based on a foreign key.
Is there a way to generate a hierarchy based on foreign/m2m key
instead of date?

Consider the following example-

class Book(models.Model):
# ...
publisher = models.ForeignKey(Publisher)

On Book's admin page, I want the books to be arranged by their
publishers. This may be helpful when I have a large number of books in
the database. How can one do this, preferably in a generic way?

Thanks,
Ram

--~--~-~--~~~---~--~~
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: automatically generated field in admin

2008-10-01 Thread Ramashish Baranwal

>
> > I have a model in which a field should be generated automatically. So
> > it shouldn't be shown in the admin add page. Also in the change page
> > it should be shown but should be non-editable. As an example-
>
> Pending ticket 342, another way to do this is to make a custom admin
> change_form template for the particular model (meaning you create
> admin/appname/modelname/change_form.html), inherit from the main
> change_form template (admin/change_form.html), and define the
> "after_field_sets" block.  You can refer to your model instance via
> the variable "original" and display whatever attribute(s) you want.
> The disadvantage of this method is that you can't control where it
> appears in the page, it will appear after the fieldset box.
>

Thanks everyone for the replies. I was looking for a way that works in
a generic way. I found one way at http://www.djangosnippets.org/snippets/937/
that works for any model. In case anyone is interested, here is how I
did it-

class StudentAdmin(ReadOnlyAdminFields, admin.ModelAdmin):
list_display = ('first', 'last', 'roll')
fields = ['first', 'last', 'roll']
readonly = ('roll',)

It does show up in the "add page", but as a non-editable field. I also
overrode the save method to populate roll in case it wasn't populated.

Ram

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



automatically generated field in admin

2008-10-01 Thread Ramashish Baranwal

Hi,

I have a model in which a field should be generated automatically. So
it shouldn't be shown in the admin add page. Also in the change page
it should be shown but should be non-editable. As an example-

class Student(models.Model):
first = models.CharField(max_length=64)
last = models.CharField(max_length=64)
# roll number, automatically generated, non-editable
roll = models.CharField(max_length=64)

setting editable=False will hide it from both add and change pages, so
I can't use that. I looked at ways for making the field non-editable
and visible, but couldn't find any satisfactory answer.

Any ideas?

Thanks in advance,
Ram

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



Admin and ManyToManyField

2007-08-02 Thread Ramashish Baranwal

Hi,

I have a model that has a ManyToManyField. When I try to add/edit an
object of this model in admin, it says that the ManyToManyField is
required i.e. I can not leave it out. While, from the shell I can
easily create an object without adding anything in the
ManyToManyField. The same happens for ForeignKey also even though I
have set null=True. Is this by design? I am using version 0.96

Thanks,
Ram


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



Binary data in CharField.

2007-06-24 Thread Ramashish Baranwal

Hi,

Apologies if its mentioned in the documentation. I would like to know
whether I can put binary data in a CharField?

Thanks,
Ram


--~--~-~--~~~---~--~~
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: Comparing dates..

2007-06-20 Thread Ramashish Baranwal

On Jun 20, 4:36 am, oggie rob <[EMAIL PROTECTED]> wrote:
> I could be a bit more pythonic:
>
> >>> from django.db.models.query import QOr
> >>> ql = [Q(birthdate__day=(startdate + timedelta(days=x)).day) & 
> >>> Q(birthdate__month=(startdate + timedelta(days=x)).month) for x in 
> >>> range(7)]
> >>> Person.objects.filter(QOr(*ql))
>
>  -rob
>
> On Jun 19, 2:51 pm, Tim Chase <[EMAIL PROTECTED]> wrote:
>
> > > I want to compare dates in my db while ignoring the year field. Lets
> > > say I have a birthday field and want to find upcoming birthdays in the
> > > next one week. How can I achieve this? If I try-
>
> > > last_date = datetime.now().date() + timedelta(days=7)
> > > users = User.objects.filter(birthday__day__lte = last_date.day)
>
> > This is a bit tricky, especially because of boundary conditions
> > like ends-of-months and end-of-years.  Note that if it's
> > currently December 28th, last_date.day isn't quite what you want
> > to be comparing to.
>
> > This can be done on the DB-server-side if you do server-specific
> > code; it can be done on the Django side in homogenous Python code
> > if you're willing to slurp over all your germane birthdays and
> > filter them locally; or, with such a small range of days (fixed
> > at 7), you could do something hackish like:
>
> >   now = datetime.now().date()
> >   dates = [now + timedelta(days=x) for x in xrange(8)]
> >   from operator import or_
> >   params = reduce(or_, [
> > Q(birthday__day=date.day, birthday__month=date.month)
> > for date in dates
> > ])
> >   users = User.objects.filter(*params)
>
> > This gets a little unweildy if you have more than a couple days
> > to consider (I don't think I'd do this for more than about 10
> > days).  It basically builds something of the form
>
> >   ...filter(Q(birthday__day=now.day, birthday__month=now.month) |
> > Q(birthday__day=now.day + timedelta(days=1),
> >   birthday__month=now.month + timedelta(days=1)) |
> > Q(birthday__day=now.day + timedelta(days=2),
> >   birthday__month=now.month + timedelta(days=2)) |
> > Q(birthday__day=now.day + timedelta(days=3),
> >   birthday__month=now.month + timedelta(days=3)) |
> > ...
>
> > for each of your days.  (thus a cap on 7 days is good...smaller
> > is better)
>
> > However, the alternative on the server side would be to write
> > DB-specific code doing some crazy date math using functions like
> > MySQL's DateAdd function.  The high-level picture would involve
> > normalizing the field's date to the year that falls within the
> > given range and then comparing it with BETWEEN to ensure it falls
> > between the two calculated dates.  Something like this 100%
> > untested code:
>
> >now = datetime.now().date()
> >end = now + timedelta(days=7)
> >users = User.objects.extra(where="""
> >  adddate(birthday, interval
> >(Extract(year from %s) - Extract(year from birthday))
> >years)
> >BETWEEN %s AND %s
> >  """,
> >  params = [now, now, end])
>
> > You might have to tweak it to add 1 to the year if there's some
> > odd boundary straddling.

Thanks folks. I will go with the db-neutral pythonic approach.

Ram


--~--~-~--~~~---~--~~
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: How to setup Django on Apache with correct rights and owners

2007-06-20 Thread Ramashish Baranwal

On Jun 20, 8:29 pm, Pythoni <[EMAIL PROTECTED]> wrote:
> I use Django with Apache and it runs great.
> I have root access to the server.
> But still I am not sure what are the best raccess rights for :
> settings.py
> for files in
> models and  views directories and for directories themselves.
> What owner shall I use? root or httpd or different?
> Thank you for help
> L.

If you are not going to modify your application directories/files
through apache, a read permission for apache would be sufficient. Else
you may have to give apache write permission.

Ram


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



Comparing dates..

2007-06-19 Thread Ramashish Baranwal

Hi,

I want to compare dates in my db while ignoring the year field. Lets
say I have a birthday field and want to find upcoming birthdays in the
next one week. How can I achieve this? If I try-

last_date = datetime.now().date() + timedelta(days=7)
users = User.objects.filter(birthday__day__lte = last_date.day)

it throws an error that it can't resolve birthday__day into field. The
exact comparison i.e. birthday__day=some_day works though.

Thanks,
Ram


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



Case insensitive urls

2007-06-01 Thread Ramashish Baranwal

Hi,

I would like to make urls of my site case-insensitive. As I
understand, by default the url matching is case-sensitive. Is there a
way to specify in my urls.py that I want a case-insensitive match?

Thanks,
Ram


--~--~-~--~~~---~--~~
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: How to delete a session

2007-05-23 Thread Ramashish Baranwal

> On May 23, 10:18 pm, "Joseph Heck" <[EMAIL PROTECTED]> wrote:
>
> > The session is a model in Django - so you can delete it like any other -
>
> > x=request.session
> > x.delete()
>
> > There's also the daily_cleanup.py script included with Django that
> > just goes straight to the 
> > database:http://code.djangoproject.com/browser/django/trunk/django/bin/daily_c...
>
> Thanks Joe, but thats not working. I'm getting the following error-
>
> AttributeError
> 'SessionWrapper' object has no attribute 'delete'
> Exception Type: AttributeError
> Exception Value:'SessionWrapper' object has no attribute 'delete'

Sorry for the multiple postings. It was due to some network problem at
my end.

-Ram


--~--~-~--~~~---~--~~
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: How to delete a session

2007-05-23 Thread Ramashish Baranwal

On May 23, 10:18 pm, "Joseph Heck" <[EMAIL PROTECTED]> wrote:
> The session is a model in Django - so you can delete it like any other -
>
> x=request.session
> x.delete()
>
> There's also the daily_cleanup.py script included with Django that
> just goes straight to the 
> database:http://code.djangoproject.com/browser/django/trunk/django/bin/daily_c...
>

Thanks Joe, but thats not working. I'm getting the following error-

AttributeError
'SessionWrapper' object has no attribute 'delete'
Exception Type: AttributeError
Exception Value:'SessionWrapper' object has no attribute 'delete'

-Ram


--~--~-~--~~~---~--~~
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: How to delete a session

2007-05-23 Thread Ramashish Baranwal

On May 23, 10:18 pm, "Joseph Heck" <[EMAIL PROTECTED]> wrote:
> The session is a model in Django - so you can delete it like any other -
>
> x=request.session
> x.delete()
>
> There's also the daily_cleanup.py script included with Django that
> just goes straight to the 
> database:http://code.djangoproject.com/browser/django/trunk/django/bin/daily_c...
>

Thanks Joe, but thats not working. I'm getting the following error-

AttributeError
'SessionWrapper' object has no attribute 'delete'
Exception Type: AttributeError
Exception Value:'SessionWrapper' object has no attribute 'delete'

-Ram


--~--~-~--~~~---~--~~
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: How to delete a session

2007-05-23 Thread Ramashish Baranwal

On May 23, 10:18 pm, "Joseph Heck" <[EMAIL PROTECTED]> wrote:
> The session is a model in Django - so you can delete it like any other -
>
> x=request.session
> x.delete()
>
> There's also the daily_cleanup.py script included with Django that
> just goes straight to the 
> database:http://code.djangoproject.com/browser/django/trunk/django/bin/daily_c...
>

Thanks Joe, but thats not working. I'm getting the following error-

AttributeError
'SessionWrapper' object has no attribute 'delete'
Exception Type: AttributeError
Exception Value:'SessionWrapper' object has no attribute 'delete'

-Ram


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



How to delete a session

2007-05-23 Thread Ramashish Baranwal

Hi,

How can I delete the session of a request? The docs speak about using
session as a dict and setting and removing specific keys on that, but
I want to remove the session itself, something like del request.session

Any ideas?

Thanks in advance,
Ram


--~--~-~--~~~---~--~~
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: Serialization with file output

2007-05-22 Thread Ramashish Baranwal

Malcolm Tredinnick wrote:
> > I am trying to serialize some model data with the output going to a
> > file. I am doing it the way its suggested in the documentation.
> >
> > XMLSerializer = serializers.get_serializer("xml")
> > xml_serializer = XMLSerializer()
> > out = open("file.xml", "w")
> > xml_serializer.serialize(SomeModel.objects.all(), stream=out)
> >
> > This throws an AttributeError-
> > Traceback (most recent call last):
> >   File "", line 1, in ?
> >   File "/home/ram/jasmine/Django-0.95/django/core/serializers/
> > base.py", line 50, in serialize
> > return self.getvalue()
> >   File "/home/ram/jasmine/Django-0.95/django/core/serializers/
> > base.py", line 110, in getvalue
> > return self.stream.getvalue()
> > AttributeError: 'file' object has no attribute 'getvalue'
> >
> > Looking into base.py, I found that the serialize function attempts to
> > return the entire data using getvalue function of the stream it wrote
> > to. That works for StringIO which is used if one doesn't pass a
> > stream, but not for a file object.
> >
> > This doesn't look like an intentional behavior. Am I missing
> > something?
>
> This was bug #3435, fixed in [5075]. Since we only fixed it a month ago
> (April 25), the fix isn't in 0.95 or 0.96. However, you could look at
> that changeset and manually patch your local copy yourself if you wanted
> to.

Thanks Malcolm.

Regards,
Ramashish


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



Serialization with file output

2007-05-22 Thread Ramashish Baranwal

Hi,

I am trying to serialize some model data with the output going to a
file. I am doing it the way its suggested in the documentation.

XMLSerializer = serializers.get_serializer("xml")
xml_serializer = XMLSerializer()
out = open("file.xml", "w")
xml_serializer.serialize(SomeModel.objects.all(), stream=out)

This throws an AttributeError-
Traceback (most recent call last):
  File "", line 1, in ?
  File "/home/ram/jasmine/Django-0.95/django/core/serializers/
base.py", line 50, in serialize
return self.getvalue()
  File "/home/ram/jasmine/Django-0.95/django/core/serializers/
base.py", line 110, in getvalue
return self.stream.getvalue()
AttributeError: 'file' object has no attribute 'getvalue'

Looking into base.py, I found that the serialize function attempts to
return the entire data using getvalue function of the stream it wrote
to. That works for StringIO which is used if one doesn't pass a
stream, but not for a file object.

This doesn't look like an intentional behavior. Am I missing
something?

Thanks,
Ram


--~--~-~--~~~---~--~~
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: Cannot resolve keyword into field

2007-04-24 Thread Ramashish Baranwal

On Apr 24, 1:45 am, "Jason McVetta" <[EMAIL PROTECTED]> wrote:
> >   TypeError: Cannot resolve keyword 'book' into field
>
> This is a long-standing, well-known bug that apparently no one (including
> me) knows how to fix.
>
> Any time one defines a ManyToMany relationship, then calls all() on that
> relationship from a script, a "cannot resolve keyword into field" error
> results.  The problem involves some deep voodoo about how and when modules
> are imported, which is why it occurs only in scripts but not in the admin
> interface or the manage.py shell.  If you google around the django-users
> archives and bug tickets, you'll see some (imho truly hideous) hacks for
> working around it by mangling your import statements.

Jason, I have observed that if I add the directory containing my
django project to PYTHONPATH, this problem doesn't come (something
similar was mentioned in one of the tickets). For example if my
project resides in /home/ram/mysite-, adding this line at the top of
my script make it work-

sys.path.append('/home/ram')

May be, that can provide some hint.

-Ram


--~--~-~--~~~---~--~~
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: Cannot resolve keyword into field

2007-04-24 Thread Ramashish Baranwal

On Apr 22, 7:33 pm, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> Can you post the exact code throwing the error, and copy paste your
> exact model code related to the error?

The code is exactly the same as I wrote in my OP.

# models.py

from django.db import models

# Create your models here.
class Author(models.Model):
name = models.CharField(maxlength=100)

class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)

# test.py, this is used to retrieve author records of a particular
book.
import os
if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import settings
from test.models import *

b = Book.objects.get(id=1)
for a in b.authors.all(): print a

>
> It sounds like you have something like the following:
>
> .filter(book=
>
> where it should be something like
>
>  .filter(someobject__book
>
> But it's hard to tell w/out seeing  the code.
>

I am trying to get authors of a particular book, so
b = Book.objects.get(id=1) # Get some book
# Get the authors
for a in b.authors.all(): print a

However, as Jason mentioned below, it seems to be an unsolved bug. :(

-Ram


--~--~-~--~~~---~--~~
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: Cannot resolve keyword into field

2007-04-21 Thread Ramashish Baranwal

On Apr 22, 3:13 am, Ramashish Baranwal <[EMAIL PROTECTED]>
wrote:
> Hi,
>
> I using a script to fetch some database records. However, when it
> encounters a ManyToManyField it gives the following error-
>
> TypeError: Cannot resolve keyword 'book' into field
>
> My models.py looks like this-
>
> from django.db import models
>
> # Create your models here.
> class Author(models.Model):
> name = models.CharField(maxlength=100)
>
> class Book(models.Model):
> title = models.CharField(maxlength=100)
> authors = models.ManyToManyField(Author)
>
> and my test script to retrieve records contains (the app name is
> test)-
>
> import os
> if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
> os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
>
> import settings
> from test.models import *
>
> b = Book.objects.get(id=1)
> for a in b.authors.all(): print a
>
> I am using django with sqlite2 on Fedora Core 6 with python-2.4.3 and
> have tried version 0.95, 0.96 and svn trunk. All throw the same error-
>   TypeError: Cannot resolve keyword 'book' into field
> When I try the same through "python manage.py shell" interactive
> shell, i.e. by entering the two lines above, it works fine. Probably,
> I'm missing some setting option. But the same script works for models
> that don't contain an M2M field, so that seems unlikely.
>
> Any clues?

Sorry, a typo in my earlier post. Its sqlite3 not sqlite2.

Ram


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



Cannot resolve keyword into field

2007-04-21 Thread Ramashish Baranwal

Hi,

I using a script to fetch some database records. However, when it
encounters a ManyToManyField it gives the following error-

TypeError: Cannot resolve keyword 'book' into field

My models.py looks like this-

from django.db import models

# Create your models here.
class Author(models.Model):
name = models.CharField(maxlength=100)

class Book(models.Model):
title = models.CharField(maxlength=100)
authors = models.ManyToManyField(Author)

and my test script to retrieve records contains (the app name is
test)-

import os
if not os.environ.has_key('DJANGO_SETTINGS_MODULE'):
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import settings
from test.models import *

b = Book.objects.get(id=1)
for a in b.authors.all(): print a

I am using django with sqlite2 on Fedora Core 6 with python-2.4.3 and
have tried version 0.95, 0.96 and svn trunk. All throw the same error-
  TypeError: Cannot resolve keyword 'book' into field
When I try the same through "python manage.py shell" interactive
shell, i.e. by entering the two lines above, it works fine. Probably,
I'm missing some setting option. But the same script works for models
that don't contain an M2M field, so that seems unlikely.

Any clues?

Thanks,
Ram


--~--~-~--~~~---~--~~
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: Accessing model fields automatically..

2007-03-31 Thread Ramashish Baranwal

On Mar 31, 9:13 pm, "Russell Keith-Magee" <[EMAIL PROTECTED]>
wrote:
> On 3/31/07,RamashishBaranwal <[EMAIL PROTECTED]> wrote:
>
> > I am trying to export a model's data to XML. To do this I'm using the
> > model class's _meta.fields. However it doesn't list ManyToManyField
> > objects. How can I get those?
>
> Are you aware of the XML serializer? Django already has a module for
> converting the data of a model into XML (JSON and YAML are also
> supported).
>
> http://www.djangoproject.com/documentation/serialization/
>
> Even if the Django serializer doesn't meet your needs, the
> implementation does serve as an example of how to do generic model
> traversal (i.e., how to find all the fields of a model, and the values
> of those field, in a generic way).
>

Thanks Russ for pointing that out. I'll have a look at that, may be
that can save some of my time.

-Ram


--~--~-~--~~~---~--~~
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: Accessing model fields automatically..

2007-03-31 Thread Ramashish Baranwal

On Mar 31, 10:36 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Sat, 2007-03-31 at 05:27 +,RamashishBaranwal wrote:
> > Hi,
>
> > I am trying to export a model's data to XML. To do this I'm using the
> > model class's _meta.fields. However it doesn't list ManyToManyField
> > objects. How can I get those?
>
> > My code looks something like this-
>
> > # returns data of inst in a dict
> > def get_data(inst):
> >   data = {}
> >   for field in inst._meta.fields: # Doesn't get ManyToManyFields :(
> > data[field.name] = getattr(inst, field.name)
> >   return data
>
> Use inst._meta.many_to_many to get the list of many-to-many fields.
>

Thanks Malcolm, that was what I was looking for.


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



Accessing model fields automatically..

2007-03-30 Thread Ramashish Baranwal

Hi,

I am trying to export a model's data to XML. To do this I'm using the
model class's _meta.fields. However it doesn't list ManyToManyField
objects. How can I get those?

My code looks something like this-

# returns data of inst in a dict
def get_data(inst):
  data = {}
  for field in inst._meta.fields: # Doesn't get ManyToManyFields :(
data[field.name] = getattr(inst, field.name)
  return data

Thanks,
Ram


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



Query regarding Schema evolution branch.

2007-02-19 Thread Ramashish Baranwal

Hi,

I am writing an app where I will occasionally need to change my db
schema. I am not an SQL expert, and would like to confine myself to
python and django db APIs if possible (don't want to go through raw
SQL or db engine specific code). After some googling I found django's
schema evolution branch that seems to be created for this. I am having
a look at this branch, but don't have a clue as to how to use it for
migrating my db from one schema to another. I would like to know-

1. How stable this branch is
2. Has anyone used/is using this branch for any app
3. If this branch can be used for db migration, some guide or examples
towards that.

I know a fair amount of python, so if required can contribute towards
this.

Regards,
Ram


--~--~-~--~~~---~--~~
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: Restricting q QuerySet based on ManyToManyField

2007-01-29 Thread Ramashish Baranwal

> Ram-
>
> I believe that
>
> User.objects.filter(related__name__starts_with='A')
>
> would get you what you're looking for, per the example at 
>  starting
> with block "# We can perform kwarg queries across m2m relationships".

Thanks Williams.:)
My problem is solved.

-Ram


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



Restricting q QuerySet based on ManyToManyField

2007-01-28 Thread Ramashish Baranwal

Hi,

I would like to restrict a QuerySet based on its ManyToManyField. In 
particular, based on what the ManyToManyField contains. Consider this 
trivial and a bit non-sense example-

class User:
name = models.CharField(maxlength=24)
related = models.ManyToManyField('self', symmetric=False)

I want to get Users who are related to other users whose names start 
with 'A'. e.g.

pplA = User.objects.filter(name__starts_with='A')

# The below doesn't result in what I expect.
#relatedA = User.objects.filter(related__in=ppl)

I am looking for a clause that can help me accomplish this but ain't 
able to find in docs.

Thanks in advance,
-Ram


--~--~-~--~~~---~--~~
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: Restricting q QuerySet based on ManyToManyField

2007-01-28 Thread Ramashish Baranwal

On 1/28/07, Ramashish Baranwal <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I would like to restrict a QuerySet based on its ManyToManyField. In
> particular, based on what the ManyToManyField contains. Consider this
> trivial and a bit non-sense example-
>
> class User:
> name = models.CharField(maxlength=24)
> related = models.ManyToManyField('self', symmetric=False)
>
> I want to get Users who are related to other users whose names start
> with 'A'. e.g.
>
> pplA = User.objects.filter(name__starts_with='A')
>
> # The below doesn't result in what I expect.
> #relatedA = User.objects.filter(related__in=ppl)
>
> I am looking for a clause that can help me accomplish this but ain't
> able to find in docs.
>

There have been a couple of typos in my earlier Post. Sorry for that,
the correctected version is-

I would like to restrict a QuerySet based on its ManyToManyField. In
particular, based on what the ManyToManyField contains. Consider this
trivial and a bit non-sense example-

class User:
   name = models.CharField(maxlength=24)
   related = models.ManyToManyField('self', symmetric=False)

class User(models.Model):
   name = models.CharField(maxlength=24)
   related = models.ManyToManyField('self', symmetric=False)

I want to get Users who are related to other users whose names start
with 'A'. e.g.

pplA = User.objects.filter(name__starts_with='A')

# The below is for illustration only, it doesn't do what I expect.
#relatedA = User.objects.filter(related__in=pplA)

I am looking for a clause that can help me accomplish this but ain't
able to find in docs. Please note that the users in relatedA might
also be related to other users not in pplA too, all I want is they
should atleast be related to one user in pplA.

Thanks,
-Ram

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