Hi again, >From what I could understand, I would have to recreate the database so that django could create an ID field for me. Unfortunately that isn't possible because I'm using a legacy database and I can't change it. In this database, the primary key is the two fields combined, so their combination has to be unique. Is there a way for me to do this without having to change the database? Just changing the models.py file? Thanks for your help! Ana
On Jul 5, 8:26 am, AnaReis <[EMAIL PROTECTED]> wrote: > On Jul 4, 4:35 pm, RajeshD <[EMAIL PROTECTED]> wrote: > > > On Jul 4, 12:14 pm, AnaReis <[EMAIL PROTECTED]> wrote: > > > > Hi again, > > > Thanks for the suggestion, but this still isn't working... the > > > difference between this module and the others that I have already made > > > is that (besides the fact that the others actually work) this one > > > involves a table with two primary keys. If it had just one I bet there > > > would be no problem. > > > And you'd win that bet! I should have noticed the two PKs in your > > model. > > > Here's the thing: Django allows only one PK field per object. You > > should remove the two primary_key=True attributes altogether from your > > model (letting Django create a PK id field for you.) Then, use the > > unique_together clause to make the database enforce that the pair > > (level_name, instrument_name) is always unique. That clause is defined > > in your Meta class like this: > > > unique_together = (('level_name', 'instrument_name'),) # Note that > > this is a nested tuple > > > For added measure you can add a db_index=True to your formerly primary > > key fields to make querying over them faster. > > > Don't forget to wipe out the model entirely from your DB table > > (assuming you are in development mode with despensible test data) and > > then syncdb to recreate everything. > > Hi, > Is it possible to do this unique_together field without changing the > database? It's because I'm using a legacy database and I can't change > it. > To create the models I simply used: django-admin.py inspectdb > > models.py > If Django creates the PK id field for me, do you mean it will chose > from one of the fields I will have in the database or will it create a > new field? > The model I'm using is this one: > [models.py] > class Level(models.Model): > level_name = models.CharField(primary_key=True, maxlength=20) > instrument_name = models.CharField(primary_key=True, maxlength=20) > available = models.TextField() > tablename = models.CharField(blank=True, maxlength=20) > def __str__(self): > (..) > class Meta: > db_table = 'Level' > I'm sorry for all my questions but I'm still learning how to work with > Django and there are several things that I still don'tknowhow to > deal with. :) > And thanks for your help and patience! :) > Ana --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---