Friday, September 25, 2020

Re: Setting up models and postgres in django 3.1

Figured it out 3.1 is more strict about how it creates tables in postgres.  The workaround I used was to put the tables into the various appname/models.py and then import them as needed when I need them.  

On Friday, September 25, 2020 at 12:38:19 PM UTC-5 Patrick Carra wrote:
So I have worked in django 2.2 where I have multiple apps that I use the same models.py file in for all of them (same meaning I make sure each one is exactly the same as the others not one single models.py).  When I do migrations I have no problem with anything.  When I started using django 3.1 if I try to do the same it creates tables in the postgres database for  each app.  For example I have  a table called Menu_Item it creates tables for home_Menu_Item, custOrders_Menu_Item, and menu_Menu_Item.  How to get them all to use the same postgres table Menu_Item.   Why is django 3 acting differently.  I'm not sure what changed but it isn't working out well for me.

menu/models.py
from django.db import models
# Create your models here.
class Menu_Item(models.Model):
    class MenuTypes(models.TextChoices):
        APPETIZER = "Appetizer"
        ENTREE = "Entree"
        DESSERT = "Dessert"
        WINE = "Wine"
        BEER = "Beer"
        COCKTAIL = "Cocktail"
    name = models.CharField(max_length = 50)
    description = models.CharField(max_length = 250)
    price = models.DecimalField(max_digits = 4, decimal_places= 2)
    item_type = models.CharField(max_length = 10, choices=MenuTypes.choices, default=MenuTypes.APPETIZER)
      
    
    def __str__(self):
        return self.name + ' - ' + self.item_type
class Menu_Drink_Item(models.Model):
    class DrinkChoices(models.TextChoices):
        WINE = "Wine"
        BEER = "Beer"
        COCKTAIL = "Cocktail"
    name = models.CharField(max_length = 50)
    description = models.CharField(max_length = 250)
    price = models.DecimalField(max_digits = 4, decimal_places= 2)
    drink_type = models.CharField(max_length = 10, choices=DrinkChoices.choices, default=DrinkChoices.COCKTAIL)
    pairing_options = models.ForeignKey(Menu_Item, null=True, blank=True, on_delete = models.SET_NULL)
    def __str__(self):
        return self.name

custOrders/models.py
from django.db import models
from menu.models import Menu_Item
# Create your models here.
class tableTable(models.Model):
    tableTypeChoices = [
            ('bar', 'bar'),
            ('booth', 'booth'),
            ('table', 'table'),
            ('hi-top', 'hi-top'),
            ('patio', 'patio'),
            ]
    statusChoices = [
            ('cleaned', 'cleaned'),
            ('seated', 'seated'),
            ('closed', 'closed'),
            ('reserved', 'reserved'),
            ]
    #primary key field
    seats = models.IntegerField(blank=False, null=False)
    tableType = models.CharField(max_length=255, blank=False, null=False, choices=tableTypeChoices)
    status = models.CharField(max_length=255, blank=False, null=False, choices=statusChoices)
    server = models.CharField(max_length=255, blank=False, null=False)
    def __str__(self):
        return '%s %s' % (self.id, self.tableType)
class Menu_Item(models.Model):
    class MenuTypes(models.TextChoices):
        APPETIZER = "Appetizer"
        ENTREE = "Entree"
        DESSERT = "Dessert"
        WINE = "Wine"
        BEER = "Beer"
        COCKTAIL = "Cocktail"
    name = models.CharField(max_length = 50)
    description = models.CharField(max_length = 250)
    price = models.DecimalField(max_digits = 4, decimal_places= 2)
    item_type = models.CharField(max_length = 10, choices=MenuTypes.choices, default=MenuTypes.APPETIZER)
      
    
    def __str__(self):
        return self.name + ' - ' + self.item_type


Any ideas?

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10a8253c-53df-40d2-808e-d9737e5e6e42n%40googlegroups.com.

No comments:

Post a Comment