1

I feel that this is a very simple question, but I'm new to Python, and I'm learning Django at the same time.

My objective is to create a string dictionary representation i.e. it's dictionary formatted but a string, of a Model's instance in Django. How could I do that? Is there a built-in function that I can call straight from the object's instance, or do I have to define one?

UPDATE:

I would like to call this functionality within the model definition itself i.e. I'm implementing a class method or function which needs this functionality. I'm thinking of a functionality which behaves like python's built-in function locals() but should only return the model's attributes.

I also would like to add that I'll be calling this functionality on a model's instance which has not been saved yet to the database. So in essence, I'll be working on a model's instance representing a record which is not yet in the database. So anything function using a Manager or QuerySet I guess is not why I'm looking for.

Example:

class Person(models.Model):
     name = ...
     age =  ...

     def func_doing_something(self):
          #get the string dictionary representation of this model's instance
          #do something to it
          #return something

Thanks everyone!

ultrajohn
  • 2,527
  • 4
  • 31
  • 56

3 Answers3

1

Use p = Person(name='john', age=10).values()

See here: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.values

To get it to a string use:

s = str(p[0])
Mikael
  • 3,148
  • 22
  • 20
  • thank you for your quick response, but that's just the first part of what I'm trying to. I want to store the that dictionary into the database, so I'm thinking I'm gonna need to convert it to a string first. Is there other way to do this? How can I turn the dictionary representation into a string? Thank you very much! – ultrajohn Jun 06 '12 at 09:21
  • 1
    Why would you want to store the string representation in the db? That makes no sense. – Daniel Roseman Jun 06 '12 at 09:21
  • let's say I want to call another function with that string as an argument, and I need it to be a string because that function will process it as a string. :) – ultrajohn Jun 06 '12 at 09:23
  • oh, that was easy. thank you very much! I feel so dumb right now. Hahaha really, – ultrajohn Jun 06 '12 at 09:27
  • Hi, I'm sorry I have to unaccept your answer. I just realised this is not a solution to the case I'm trying to solve. Basically, I'll be working on a single instance of a model at a time, not q QuerySet, so it does not work. I would like to get the string dictionary representation of a model instance i.e. object, within the model definition itself. – ultrajohn Jun 08 '12 at 19:55
1

You can serialize your objects to json format, e.g. with django build-in serializers This allows you deserialize quite easily.

San4ez
  • 8,091
  • 4
  • 41
  • 62
  • I read that a serializer expects a QuerySet as an argument. In my case, I'll be working on a single instance of a model at a time before it is even saved into the database. Thank you! – ultrajohn Jun 08 '12 at 20:17
1

I found from this SO post the solution I was looking for...

some Django object obj

[(field.name, getattr(obj,field.name)) for field in obj._meta.fields]

and just call dict() on the result.

Community
  • 1
  • 1
ultrajohn
  • 2,527
  • 4
  • 31
  • 56