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 8512771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:22:24+00:00 2026-06-11T04:22:24+00:00

Suppose I have the following function, which retrieves data from a server in the

  • 0

Suppose I have the following function, which retrieves data from a server in the form of user-defined objects. For example, let’s define the objects as PersonalEntry.

def retrieve_people()
    // returns a list of all latest people objects, with fields equal to those in PersonEntry

def retrieve_books()
    // returns a list of all latest book objects regardless of author, with fields equal to those in BookEntry, contains attribute, 

Both user-defined classes has an .as_dict() method which returns all its attributes in a dictionary.

I would like to update the model whenever this function is called (ie. update the fields if the instance of that model already exists, else, define a new instance of the model). This is my current setup.

class PersonEntry(models.Model):
    name = models.CharField(max_length = 50)
    age = models.IntegerField()
    biography = models.CharField(max_length = 200)

    def update_persons():
         try: 
             temp = retrieve_person()
             for person in temp:
                 match = PersonEntry.objects.filter(name = person.name(), age = person.age())
                 match.update(**person.as_dict())
         except DoesNotExist:
             PersonEntry.create(**person.as_dict())


class BookEntry(models.Model):
    author = models.ForeignKey(PersonEntry)
    author_name = models.CharField(max_length = 50) //books return redundant info
    author_age = models.IntegerField() //books return redundant info
    title = models.CharField(max_length = 50)
    summary = models.CharField(max_length = 200)

    def update_books():
         try: 
             temp = retrieve_books()
             for book in temp:
                 match = BookEntry.objects.filter(title = temp.title())
                 match.update(**book.as_dict(), associate_person(book.author_age(), book.author_name()))
         except DoesNotExist:
             BookEntry.create(**book.as_dict(), associate_person(book.author_age(), book.author_name())

    def associate_person(age, name):
         return PersonEntry.get(name = name, age = age)

I suppose a more general question is, how do I update models with relationships if I have a function which returns data? Do I have a method in the model itself, or do I have it one level up (ie. move update_books to the Person model) I’m new to Django, so not really sure how the organization should be.

  • 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-11T04:22:26+00:00Added an answer on June 11, 2026 at 4:22 am

    I confess I haven’t completely grokked your question, but I’ll take a punt that you should look into

    Managers

    Generally, in django, everything is done as lazily as possible – meaning nothing gets updated until you actually try to use it – so you don’t update models/relationships as you go, rather you just declare what they are (perhaps with a manager) then it works it out the current value only when asked.

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

Sidebar

Related Questions

For example suppose I have the following two functions function a(param1) { console.log(param1); }
For example suppose I have the following app.get('/', function(req, res) { var ip; if(req.headers['x-forwarded-for']){
I have following json data which I want to pass it to server using
Let's suppose I want to create a javascript class/object/function which have a method that
Suppose you have the following function foo . When I'm running a for loop,
Suppose I have the following code function myFunction(param, callback) { ... if (err) {
Suppose I have the following (shortened for simplicity): jQuery(#grid).jqGrid({ ... ondblClickRow: function() { //
Suppose I have a function void myFun(int*) In C++ what exactly does the following
suppose we have following function: void someFunction(int * araye){ for (int i=0;i<5;i++) cout <<araye[i]<<'
Suppose I have a file insert.c in which two functions are defined: 1.insert_after 2.insert_before

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.