Different choices values for a single field in inherited class

18 views
Skip to first unread message

moogly...@gmail.com

unread,
Jul 22, 2014, 5:06:53 PM7/22/14
to django...@googlegroups.com
Hello,                                                                                                                                                                                                             
                                                                                                                                                                                                                   
I'm writing my first webapplication{, in Django} and I'm faced with a                                                                                                                                              
problem that I can't manage alone.                                                                                                                                                                                 
                                                                                                                                                                                                                   
I want to write an application for classifying my wine. There, a wine could                                                                                                                                       
be red or white and regarding this parameter a wine have a more                                                                                                                                                    
specific tone choice, let say for a red wine could be ligth red or                                                                                                                                                 
dark red...                                                                                                                                                                                                        
                                                                                                                                                                                                                   
Here is the idea I want to wrote                                                                                                                                                                                   

$ wine/models.py:
class Color(models.Model):    
       
class Meta:                                                                                                                                                                                                      abtract = true                                                                                                                                                                                    
        tone    
= models.CharField(max_length = 32, choices = SOMETHING)                                                                                                                                          
class RedColor(Color):                                                                                                                                                                                          R_COLORS = (                                                                                                                                                                                    
               
("Light red",    "light red"),                                                                                                                                                          
               
("Dark red",     "dark red")                                                                                                                                                              )                                                                                                                                                                                                            
 
# Tells the parents class that SOMETHING = R_COLORS for red wines              
 
class WhiteColor(Color):                                                                                                                                                                                        W_COLORS = (                                                                                                                                                                                                  ("Ligth white",  "ligth white"),                                                                                                                                                                        ("Dark white",   "dark white")                                                                                                                                                              )                                                                                                                                                                                                                # Tells the parents class that SOMETHING = W_COLORS for white                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
My problems are that I can't manage to write this idea in correct                                                                                                                                                  
Python/Django.                                                                                                                                                                                                     
                                                                                                                                                                                                                   
Also I'm far to be sure that this is the good way of storing this                                                                                                                                                  
information. I'm not sure that 2 tables for storing the same kind of                                                                                                                                               
data is a good way of designing databases.                                                                                                                                                                         
                                                                                                                                                                                                                   
If someone could lead me on some directions it will be great ! 

Thanks                                                                                                                                                   
                                                                                                                                                                                                                   
PS: I will have the same problems on other table on the application,                                                                                                                                               
for example the grappe variety.                                                                                                                                                                                    
                                                                                                                                                                                                                   
PPS: I will have the same problem for building the next application                                                                                                                                                
for classifying my beers.

cmawe...@gmail.com

unread,
Jul 31, 2014, 12:29:47 PM7/31/14
to django...@googlegroups.com
class Color(models.Model):
    red_or_white
= models.CharField(max_length=10, choices=[('red', 'Red'), ('white', 'White')])
    tone
= models.CharField(max_length=32)
 
class Wine(models.Model):
    name
= models.CharField(max_length=255)
    color
= models.ForeignKey(Color)    



MadEng

unread,
Mar 18, 2016, 4:07:16 PM3/18/16
to Django users
Hello Moogly,

$ wine/models.py:


class ColorBase(models.Model):    
       
class Meta:                                                                                                                             abstract = true
       
        
COLORS_CHOICHES = (
                        
("Red",    "Red"), 
                        ("Blue",   "Blue"), 
                 )
                                                                                                                                                                         
        tone= models.CharField(max_length = 32, choices = COLORS_CHOICHES)                                                                                                                                          
class RedColor(ColorBase):                                                                                                                      COLORS_CHOICHES = (                                                                                                                      ("Light red",    "light red"),                                                                                                      ("Dark red",     "dark red")                                                                                                 )                                                                                                                                  ....                                                                          
               
 
class WhiteColor(ColorBase):                                                                                                                    COLORS_CHOICHES = (                                                                                                                    ("Light white",  "light white"),                                                                                                    ("Dark white",   "dark white")                                                                                                 )                                                                                                                                   ....                                                                          

Now in your $ wine/admin.py:

class ColorBaseAdmin(admin.ModelAdmin):
     model = ColorBase
      def formfield_for_dbfield(self, db_field, **kwargs):

        if db_field.name == 'status':

            kwargs.update(

                {'choices':self.model. COLORS_CHOICHES}

            )
            #I THINK THIS IS NOT REQUIRED BECAUSE AUTOMATICALLY YOU HAVE MODEL UPDATED.. you can omit these two lines

            if settings.DEBUG self.model == ColorBase:

                logger.warning("Please set the model in your model admin to display correct status' choices")

        return super(ColorBaseAdmin, self).formfield_for_dbfield(

            db_field, **kwargs)

class RedColorAdmin(ColorBaseAdmin):
    model=RedColor
class WhiteColorAdmin(ColorBaseAdmin):

    model=WhiteColor

admin.site.register(RedColorRedColorAdmin)
admin.site.register(WhiteColor, WhiteColorAdmin)

If you need choices in your frontend form you have to pass YourColor.COLOR_CHOICES choices to your form field in your form's __init__ function
#HOPE THIS WORKS BUT I DIDN'T TEST IT

class BaseColorForm
(forms.ModelForm):
    

    def __init__(self, **kwargs):
        
self.base_fields['tone'].choices=self.Meta.model.COLOR_CHOICES

        super(BaseColorForm, self).__init__(*args, **kwargs)

class RedColorForm(BaseColorForm):
    class Meta:
        model = RedColor

class WhiteColorForm(BaseColorForm):
    class Meta:
        model = 
WhiteColor





Hope this helps

Reply all
Reply to author
Forward
0 new messages