Creating JSON file

2013-04-08 Thread Seth Gordon
You want to output a list of one dict per object, and you want that dict to
contain another dict inside it. So you don’t need dict2. Instead

f = file('report.json','a+')
sys.stdout = f
result = []
objects = game_objects.objects.all()
for obj in objects:
time = time_stamp.objects.filter(user_id = obj.user_id)
obj_id = obj.object_id
per = performance.objects.filter(user_id = obj.user_id)
pat = pattern.objects.filter(user_id = obj.user_id)
dict1 = {'user_id' : obj.user_id, 'game_id': obj.game_id}
dict1[obj_id]={'color': obj.color, 'speed': obj.speed, 'energy':
obj.energy }
result.append(dict1)


On Mon, Apr 8, 2013 at 8:33 AM, Nagarajan Dharmar Sitha <
nagarajan.dharmarsi...@gmail.com> wrote:

> saved = sys.stdout
> f = file('report.json','a+')
> sys.stdout = f
> result = []
> objects = game_objects.objects.all()
> for obj in objects:
> dict2 ={}
> time = time_stamp.objects.filter(user_id = obj.user_id)
> obj_id = obj.object_id
> per = performance.objects.filter(user_id = obj.user_id)
> pat = pattern.objects.filter(user_id = obj.user_id)
> dict2[obj_id]={'color': obj.color, 'speed': obj.speed, 'energy':
> obj.energy }
> dict1 = {'user_id' : obj.user_id, 'game_id': obj.game_id}
> result.append(dict1)
> result.append(dict2)
>
> print json.dumps(result, indent =  4)
> sys.stdout = saved
>
> this is my code and out put of the code is:
>
>
>
> [
> {
> "game_id": "000g1",
> "user_id": "000u1"
> },
> {
> "000o1": {
> "color": "red",
> "energy": "300",
> "speed": 2
> }
> },
> {
> "game_id": "000g2",
> "user_id": "000u2"
> },
> {
> "000o2": {
> "color": "red",
> "energy": "450",
> "speed": 6
> }
> },
> {
> "game_id": "000g3",
> "user_id": "000u3"
> },
> {
> "000o3": {
> "color": "red",
> "energy": "600",
> "speed": 12
> }
> },
> {
> "game_id": "000g4",
> "user_id": "000u4"
> },
> {
> "000o4": {
> "color": "red",
> "energy": "750",
> "speed": 20
> }
> },
> {
> "game_id": "000g5",
> "user_id": "000u5"
> },
> {
> "000o5": {
> "color": "red",
> "energy": "900",
> "speed": 30
> }
> },
> {
> "game_id": "000g6",
> "user_id": "000u6"
> },
> {
> "000o6": {
> "color": "red",
> "energy": "1050",
> "speed": 42
> }
> },
> {
> "game_id": "000g7",
> "user_id": "000u7"
> },
> {
> "000o7": {
> "color": "red",
> "energy": "1200",
> "speed": 56
> }
> },
> {
> "game_id": "000g8",
> "user_id": "000u8"
> },
> {
> "000o8": {
> "color": "red",
> "energy": "1350",
> "speed": 72
> }
> },
> {
> "game_id": "000g9",
> "user_id": "000u9"
> },
> {
> "000o9": {
> "color": "red",
> "energy": "1500",
> "speed": 90
> }
> }
> ]
>
>
> but i want out put like this way:
>
>
> [
> {
> "game_id": "000g1",
> "user_id": "000u1"
> "000o1": {
> "color": "red",
> "energy": "300",
> "speed": 2
> }
> },
> {
> "game_id": "000g2",
> "user_id": "000u2",
> "000o2": {
> "color": "red",
> "energy": "450",
> "speed": 6
> }
> },
> ..
> .
> ]
>
>
> kindly help me out...
>
>  --
> 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 http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
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 http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




removing the source of a OneToOneField relationship

2011-05-03 Thread Seth Gordon
I have one Django model that points to another one with a OneToOneField,
sort of like this:

class Target(models.model):
# stuff

class Source(models.Model):
target = models.OneToOneField(Target)

Sometimes, given an object that is an instance of Target, I want to
navigate to its Source and delete that:

try:
some_target.source.delete()
except ObjectDoesNotExist:
## nothing to delete!
pass

But this doesn't work: some_target.source will still exist after the
delete() operation has been performed.  If I try "some_target.source =
None", I get an exception, complaining that Target.source does not allow
null values.  Calling save() and clean_fields() at strategic moments
doesn't seem to work, either.

What does seem to work is reloading the Target object:

try:
some_target.source.delete()
some_target = Target.objects.get(pk=some_target.id)
except Source.DoesNotExist:
## nothing to delete!
pass

Is this a bug in Django, or am I misunderstanding how related or cached
objects are supposed to work?

This is Django 1.2.3, Python 2.6.5, PostgreSQL 8.4.7, all running on
Ubuntu Linux 10.04.

-- 
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: removing the source of a OneToOneField relationship

2011-05-04 Thread Seth Gordon
On 05/04/2011 12:43 AM, Andy McKay wrote:
> 
> On 2011-05-03, at 2:05 PM, Seth Gordon wrote:
>> I get an exception, complaining that Target.source does not allow
>> null values.
> 
> You'll probably want to allow null values on your OneToOne field then:
> 
> http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.null

Ah, but I don’t want Source.target to be a nullable value.  Every Source
should point to a Target, but not every Target needs to have a Source.

> --
>   Andy McKay
>   a...@clearwind.ca
>   twitter: @andymckay
>  
> 

-- 
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: django test framework with no models.py

2010-12-30 Thread Seth Gordon
On 12/27/2010 09:58 AM, Henrik Genssen wrote:
> Hi,
> 
> I wanted to write a test for an app, that does not have a models.py itself.
> Doing so I get:
> django.core.exceptions.ImproperlyConfigured: App with label service could not 
> be found
> 
> if I create a models.py with just a "pass" in it everything works as expected.
> I use Django 1.1

I worked around the same problem (in 1.2, at least) with just an empty
models.py file—I didn’t even need the “pass”.

-- 
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: Japanese Text Sort Order

2011-01-05 Thread Seth Gordon
On 01/05/2011 07:57 AM, James Hancock wrote:
> I think it does the same thing, but I was talking about how you cant set
> 'ordering' under the Meta class in a model.
> http://docs.djangoproject.com/en/1.2/ref/models/options/#django.db.models.Options.ordering
> 
> If
> I set it to a field of Japanese characters, the ordering comes out wrong. At
> least not in the desired order.
> How can I set the data collation for just a few fields? And how can I know
> which of the Japanese to set it to?

Does the “strcoll” function in Python’s standard “locale” module do the
right thing when a Japanese locale is set?  If not, can you persuade the
PyICU package to do the right thing?

If either of these works, then you could slurp your results into a
Python list and then sort the list using one of these methods.  This
wouldn’t perform too well, of course, if you had thousands and thousands
of results and you wanted the database to peel off the top ten... but if
for some reason collation at the database level isn’t working, doing it
all at the Python level might be better than nothing.

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