Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8063187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:56:29+00:00 2026-06-05T10:56:29+00:00

I feel that this is a very simple question, but I’m new to Python,

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:56:31+00:00Added an answer on June 5, 2026 at 10:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel like this is a simple question, but I am still relatively new
I feel like the answer to this question is really simple, but I really
I think this may be a very simple question, but I am only just
I know this question is very similar to this one, but I feel its
I'm very new to sweave and I feel this will be an easy question,
I feel like this is a very noob question.. but I just can't get
I think this has to be a very simple question but I am not
I feel that it should be something very simple and obvious but just stuck
I feel silly asking this question... Sometimes I have a domain that I want
I feel that both are the same thing, but I am not sure.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.