4

I am just starting out with Django and would like to know the best way to deal with the following data. I have DataSets which are comprised of many x,y coordinate pairs which I wish to plot. It is my understanding that Django doesn't support numeric arrays directly in it's models so what is the best way to deal with these? Right now all I can think of is something like I have below:

class DataSet(models.Model):
    set_name = models.CharField(max_length=100)

class DataPoint(models.Model):
    x = models.FloatField()
    y = models.FloatField()
    dataset = models.ForeignKey(DataSet)

This seems a bit odd and without having any experience with databases or django I am not sure how to proceed. I am using postgresql right now which I believe does support array entries but I am not sure if I am prepared to make a custom field in Django.

2 Answers 2

3

Short of a custom field your idea of defining DataPoint and DataSet models seems to be the way to go. You should consider changing the relationship between the two to a many to many field if there is the possibility of a data point occurring in more than one data set.

It would also help to write (with tests) a thin business layer combining the two to minimize the need to think in terms of how the models are stored in the database.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the Many-to-many model relationship.

From django docs:

from django.db import models

class Topping(models.Model):
    # ...
    pass

class Pizza(models.Model):
    # ...
    toppings = models.ManyToManyField(Topping)

More info: https://docs.djangoproject.com/en/2.1/topics/db/models/#many-to-many-relationships Example: https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.