New user - struggling with database design

43 views
Skip to first unread message

Jason G

unread,
Aug 28, 2014, 9:46:58 AM8/28/14
to django...@googlegroups.com
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!

Gerald Klein

unread,
Aug 28, 2014, 9:55:44 AM8/28/14
to django...@googlegroups.com
their order if I am understanding you right does not effect this, the last part is just  a join between the tables, you don't have to create another table


--
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...@googlegroups.com.
To post to this group, send email to django...@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.



--

Gerald Klein DBA

Cont...@geraldklein.com

www.geraldklein.com

geraldklein.wordpress.com

j...@zognet.com

708-599-0352


Arch, Gentoo I3, Ranger & Vim the coding triple threat.

Linux registered user #548580 

Brought to you by the Amish Mafia

Jason G

unread,
Aug 28, 2014, 10:12:37 AM8/28/14
to django...@googlegroups.com, j...@zognet.com
Hi Gerald - thanks for the quick reply! Turns out it was just an issue with quotes...not sure I am doing it right just yet, but at least I've passed the error.

Jason G

unread,
Aug 28, 2014, 12:05:01 PM8/28/14
to django...@googlegroups.com
Ok - here is where I am stuck now. Any thoughts?

If I try to render {{ vessel.CurrentBrew }} in a view, I get something like this:
<django.db.models.fields.related.ManyRelatedManager object at 0x7f76f0088e10>

and this errors out:

{% for Brew in vessel.CurrentBrew.all %}X{{ Brew.ActualName }}<br/>{% endfor %}

(1146, "Table 'dbLunawireBrewing.brewlog_vessel_CurrentBrew' doesn't exist")


The vessel model looks like this:
class Vessel(models.Model):
    Name = models.CharField(max_length=20)
    ShortName = models.CharField(max_length=4)
    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.ManyToManyField('Brew', related_name="brew")
    def __unicode__(self):
        return self.Name

Derek

unread,
Sep 2, 2014, 4:44:53 AM9/2/14
to django...@googlegroups.com
That looks like a database error - check in your database as to which tables have been created.
Reply all
Reply to author
Forward
0 new messages