What is proper way to link django-treebeard model with another django model as foreignkey?
Asked Answered
M

0

6

I'm building a system which will show important facilities around every stadium in a city like depots,railway stations, hospitals, restaurants and bars etc and their distances from respective stadium. I've two models in Django, one for Stadium and another for facilities around stadium. Facilities can be hierarchical if there are multiple options for the same. E.g.

Stadium-1:
    -Hospital:
        - some hospital: 700 meters
    -Restaurant:
        - Chinese:
            - abc restaurant: 2km
        - Italian:
            - lmn restaurant: 300 meters
            - xyz restaurant: 1.2km

The name of root node for each tree will be same as the name of stadium to which it'll be linked as foreignkey. Every stadium will have its own tree specifying important facilities around it and their distances from stadium.

I am using Django-Treebeard for the purpose of creating facilities tree. My facilities model is MP_Node type. I'm using admin interface to add my data.

When I try to link Stadium with facilities tree, it shows every node that was ever added in all tree models. This is messy. Imagine when data grows. It'll be a trouble.

I've tried to link it as OneToOneField but nothing changed. At present, I am using foreignkey to link both models. Below are my actual models:

######################## models.py ##################################
class NearByFacilities(MP_Node):
    name = models.CharField(max_length=65, blank=True, null=True)
    distance = models.DecimalField(max_digits=5, decimal_places=2, blank=True, 
    null=True)
    units = (
            ('km', 'KiloMeters'),
            ('m', 'meters')
    )
    unit = models.CharField(max_length=3, choices=units, blank=True, 
    null=True)

    def __str__(self):
        if self.name:
            return (self.name)
        else:
            return str(self.distance)





class Stadium(models.Model):
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    name = models.CharField(max_length=65)
    capacity = models.PositiveIntegerField()
    built = models.CharField(max_length=20, blank=True, null=True)
    near_by_facilities = models.ForeignKey(
        NearByFacilities, related_name='stadium', on_delete=models.CASCADE)

    def __str__(self):
        return self.name





##################### admin.py file: ###################################

from __future__ import unicode_literals
from django.contrib import admin

from treebeard.admin import TreeAdmin
from treebeard.forms import movenodeform_factory
from .models import (
    Stadium,
    NearByFacilities
)


class NearByFacilitiesAdmin(TreeAdmin):
    fields = ('name', 'distance', 'unit',
              '_position', '_ref_node_id',)
    form = movenodeform_factory(NearByFacilities)


admin.site.register(Stadium)
admin.site.register(NearByFacilities, NearByFacilitiesAdmin)


I've screenshots showing what happens when linking facilities tree to its respective stadium: 1. When adding node:

enter image description here

  1. Final Facilities Tree:

    enter image description here

  2. When Linking with respective Stadium: **enter image description here**

It shows all the nodes in all the trees. I know I'm doing something wrong here.

My questions is:
What is the proper way here? Is there problem with the way I've structured models? Is there any alternative approach for this purpose?

Maemaeander answered 24/6, 2019 at 23:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.