Thursday, August 28, 2014

New user - struggling with database design

Hi all,

I am building an app to track my home brewing.

I have two tables, one for vessels, and another for brews.

I want the Brew table to have a pick-list, relation to Vessels so I can say what vessel the brew is in.

I also want to render a table that shows all Vessels and what Brew is in each. There should be a one-to-one relationship until the Brew is 'Kicked', which could be a vessel or maybe a null value in the Brew table.

My code currently doesn't work, but looks something like this:

class Vessel(models.Model):
    Name = models.CharField(max_length=20)
    Gallons = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True)
    Weight = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True)
    CurrentBrew  = models.ForeignKey(Brew, db_column='Vessel')  <--------------------------------------this is what I'm trying to add
    def __unicode__(self):
        return self.Name

class Brew(models.Model):
    ...
    Name = models.CharField(max_length=40, null=True, default='TBD')
    InformalName = models.CharField(max_length=40)
    Status = models.IntegerField(max_length=1, choices=STATUS_CHOICES, default=1, null=False, blank=False)
    Gallons = models.DecimalField(max_digits=4, decimal_places=2, null=True, blank=True)
    Vessel = models.ForeignKey(Vessel)
    Style = models.ForeignKey(Style)
    Event = models.CharField(max_length=40, null=True, blank=True)

In this model, which I'm not sure is the best way to do it, the Vessel class does not yet know about the Brew model that is below it.

I'm starting to grasp this language (and love it, coming from doing asp/sql several years ago) but I'm stuck here. Help!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dd40f53a-c398-4806-b09d-1605d3f15ead%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment