Home > Net >  What model/approach to use to relate space and objects in django
What model/approach to use to relate space and objects in django

Time:03-07

I'm trying to play a little with django so I've started to create a "solution" to an old problem I have. My background at OO programming and models relation is not that much so I ask for ideas/paths/solutions on how to achieve my goals.

My problem:

  • I need to define spatial objects and relate them ex: Building 1, inside we have floor 1 and 2, inside floor 2 we have room A and B

  • At each type of "location" I will place "things" ex: place 1 camera at building 1; place 1 camera at floor 2; place 1 camera at room B

  • So I can archive the results bellow: Building 1: 3 cameras floor 2: 2 cameras room B: 1 camera

I was planning to archieve this using the old method of a table for each kind of object (building, floor, room) but I'm a little stuck on how to deal with diferent stuff the same way (actions) specialy because after dealing with this I have to put "people" at the equation, ie, I can put person J at room 2 and put a camera on him.

I thank you all in advance on all the thoughts (even if it is to say forget it and code it) because I a little lost here :)

Sorry if this is not the correct way (1st post): @Amun_Re I'm not that good with words, specially in English. The models you posted where my first database object relation, but after discovering the many to many relation I was thinking (and because I need to put objects in room and floor and building and they are differente) that django/python had a better way to deal with this. I'll try to post an image of what I have designed for this phase.

CodePudding user response:

Try starting with this:

models.py:

class Building(models.Model):
    #Building 1, 2 etc... Use Charfield if you want string name
    building_name = models.IntegerField()
    def __str__(self):
        return '{}'.format(self.building_name)
    
class Room(models.Model):
    #One Building can have many rooms -> One to many relationship -> Foreignkey
    building = models.ForeignKey(Building, on_delete=models.CASCADE)
    room_name = models
    def __str__(self):
        return '{}'.format(self.room_name)

#Since you want to put items such as camera on Objects (person can also be an object):
class ObjectInRoom(models.Model):
    room = models.ForeignKey(Room)
    object_in_room_name = models.CharField(max_length=256)
    def __str__(self):
        return '{}'.format(self.object_in_room_name)

#Items such as camera etc. 
class ItemOnObject(models.Model):
    object_in_room = models.ForeignKey(ObjectInRoom, on_delete=models.CASCADE)
    item_on_object_name = models.CharField(max_length=256)

    def __str__(self):
        return '{}'.format(self.item_on_object_name)

admin.py:

from django.contrib import admin
from .models import Building, Room, ObjectInRoom, ItemOnObject
# Register your models here.
admin.site.register(Building)
admin.site.register(Room)
admin.site.register(ObjectInRoom)
admin.site.register(ItemOnObject)

Don't forget to make migrations using python manage.py makemigrations and python manage.py migrate in your console.

You can test your models by running the server with python manage.py runserver and logging in to /admin. If you haven't already, create a superuser in order to be able to log in to the admin site using python manage.py createsuperuser.

CodePudding user response:

relational draft

The image above is my first design, now I changed it a little becuase I found some "stuff/library" that work different in python/django.

Let me try to explain my doubts a little better: Is there a way to relate objects (like a camera) with other different kind of objects (building, room, person) without the need to add extra code for that?

NOTE: Because I just open the account there are a lot of stuff I can't do :) sorry for messing a little my own question.

  • Related