-3

I have a table1 , with col_a, col_b, col_c,col_c. I have another table2, with tb_col1, tb_col2, tb_col3, i want to reference col_a ->tb_col, col_b ->tb_col2 , col_c -> tb_col3.

should I use composite keys, If so how do i implement this in Django(python)

my Models :

class product_models(models.Model):
     products = models.ForeignKey('products')
     model_name = models.CharField(max_length=50)
     model_price = models.IntegerField(max_length=4)
     model_desc = models.TextField(blank=True)
     commision = models.IntegerField(max_length=3)

    def __unicode__(self):
        return self.model_name



class sales_process(models.Model):

     prospect = models.ForeignKey('prospect')
     employee = models.ForeignKey(User)
     first_call = models.DateTimeField
     product = models.ForeignKey('products')
     model_name = models.ForeignKey('product_models')
     #price = reference to product_model for the price
     #commission = reference to product_model for commission 

Here how can i refer price to product_models and commission to product_models

rgm
  • 1,241
  • 2
  • 16
  • 33
  • Review [the model layer](https://docs.djangoproject.com/en/1.5/#the-model-layer) in the Django docs. Edit your question with the code you have attempted to develop to implement your tables, and then you may have a better chance of getting help. – Joseph Paetz Oct 21 '13 at 18:55
  • @Joseph Paetz I have updated the question for more clarity – rgm Oct 21 '13 at 19:22

1 Answers1

0

Why not just related sales_process to a product_models like so:

product_model = models.ForeignKey('product_models')

No need to duplicate multiple columns from product_models on the sales_process model is there?

Also, take a look at custom Model Managers: https://docs.djangoproject.com/en/dev/topics/db/managers/

You can create convenience methods on a model (to say look up things from a related model) in a nicely named way.

In the example below you can call Team.objects.get(pk=1).games() returns all games for a team, home or away.

class TeamManager(models.Manager):
    def games(self, name):
        return Game.objects.filter(home__name=name) | Game.objects.filter(away__name=name)


class Team(models.Model):
    objects = TeamManager()
    name = models.CharField(max_length=100, unique=True)

class Game(models.Model):
    away = models.ForeignKey(Team, null=True, blank=True, related_name="away")
    home = models.ForeignKey(Team, null=True, blank=True, related_name="home")  
mconlin
  • 8,169
  • 5
  • 31
  • 37
  • That's a good point to note. But for the sake , what if i still want to have duplicates? – rgm Oct 21 '13 at 20:14
  • I really don't think you want to do that. Could you explain why you think that is necessary? – mconlin Oct 21 '13 at 21:14
  • I am planning to make `sales_process` as a database view , where i do create tables , but output from joining two tables. – rgm Oct 21 '13 at 21:27
  • Does your RDBMS allow inserts and updates to views? Do you need the view to exist for any reason other than making the model look the way you want in Django? – mconlin Oct 21 '13 at 21:30
  • I doubt , As fas as i know , it is not possible, there is typo mistake in comment above, I do not want to create table again ,crete view from 2 or more tables. and yes i want the view to exist. – rgm Oct 21 '13 at 21:47
  • I still am not sure thats the way you want to go but here is a post about using views to back models, there are some drawbacks apparently but others have done it successfully. http://stackoverflow.com/questions/507795/can-i-use-a-database-view-as-a-model-in-django/508013#508013 – mconlin Oct 21 '13 at 21:49