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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:57:57+00:00 2026-05-15T01:57:57+00:00

I have the following Django Model which retrieves 3 records from a database. The

  • 0

I have the following Django Model which retrieves 3 records from a database. The class below represents a Model within a Django application I’m building. I realize that the parameters taken in by the create_hotspots function are not being used. I just simplified what the code looked like previously for the purposes of explaining my problem.

from django.db import models
from django.db import connection, transaction
import math
import MySQLdb

class Victims(models.Model):

    def __init__(self):
        self.results=[]
    def create_hotspots(self,radius,latitude,longitude):

        self.radius=str(radius)
        self.latitude=str(latitude)
        self.longitude=str(longitude)

        db=MySQLdb.connect (host = "localhost", user = "root",passwd = "pass",db = "test")
        cursor = db.cursor ()
        cursor.execute("""SELECT * FROM poi_table""")
        self.results=cursor.fetchall()
        cursor.close ()
        db.close ()
    def __unicode__(self):
        return self.results

Now, assume that I open the Django shell and I execute the following instructions:

>>from PyLayar.layer.models import Victims
C:\Python26\lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated
  from sets import ImmutableSet
>>> v=Victims()
>>> v.create_hotspots(1000, 42.3931955679, -72.5289916992)
>>> Victims.objects.all()
[]

My question is why does the Victims.objects.all() instruction return no results. The SQL query returns 3 results, and the results variable stores the resulting tuple.

  • 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-05-15T01:57:58+00:00Added an answer on May 15, 2026 at 1:57 am

    The Victims.create_hotspots method has no return statement. What did you expect it to return?

    Also, Victims.create_hotspots does not do a save() to save the Victims instance.

    BTW, the use of raw SQL inside a models object is often a really poor idea. You should consider making your “poi_table” a proper part of the Django model and using proper relational database design to avoid querying one class while trying to create an instance of another.

    If you are trying to create “persistent aggregate” objects, you should consider doing something different.

    v= Victim.objects.create( radius, lat, lon, Poi.objects.all())
    v.save()
    

    This will disentangle your two models allowing you to write simpler, less heavily-entangled models using simple Django processing.

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

Sidebar

Related Questions

In a django application I have the following model: class Appointment(models.Model): #some other fields
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following model in Django: class Bout (models.Model): fighter_1 = models.ForeignKey(Fighter, related_name=bout_fighter_1)
I have the following django model (mapped to table 'A'): class A(models.Model): name =
I have a MySQL database called People which contains the following schema <id,name,foodchoice1,foodchoice2> .
I have a model which is accessible through the Django admin area, something like
I have a model which can be attached to to other models. class Attachable(models.Model):
I have following models relation: class Section(models.Model): section = models.CharField(max_length=200, unique=True) name = models.CharField(max_length=200,
Consider the following Django models: class Host(models.Model): # This is the hostname only name
I have following Code Block Which I tried to optimize in the Optimized section

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.