Re: Testing many-to-many models on a legacy database
On Jul 11, 4:23 pm, derek wrote: > On Jul 10, 4:40 pm, Rolando Espinoza La Fuente > wrote: > > > > > On Sat, Jul 10, 2010 at 9:31 AM, Derek wrote: > > > Running Django 1.2.1 under Python 2.6. > > > I am obviously missing something cruccial, but I am just not sure where > > > this > > > is... > > > I have a setup which includes a number of many-to-many models in a legacy > > > database. For example: > > > > class Grouping(models.Model): > > > id = models.AutoField(primary_key=True, db_column='GroupingID') > > > name = models.CharField(max_length=250, db_column='GroupingName', > > > unique=True) > > > class TaxAgreement(models.Model): > > > id = models.AutoField(primary_key=True, db_column='TaxID') > > > title = models.CharField(max_length=100, db_column='TaxTitle', > > > unique=True) > > > groupings = models.ManyToManyField(Grouping, > > > db_table='taxagreementgrouping') > > > Use through > > keywordhttp://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.mod... > > > groupings = models.ManyToManyField(Grouping, through='TaxAgreementGrouping') > > Thanks, this fixes those problems. Unfortunately, however, it also > introduces a new error. When I try and start the app, I now get: > > ImproperlyConfigured at /admin/ > 'TaxAgreementAdmin.fieldsets[6][1]['fields']' can't include the > ManyToManyField field 'groupings' because 'groupings' manually > specifies a 'through' model. > > I need to be able to use the "many to many" box in order to create the > associations... > As a follow-up, I see from the docs that the many to many widget _cannot_ be used in a case where "through" is specified, because the assumption is that extra fields will be added to the join model. This is _not_ the case for me, so this implies I should not have to use the "through" option to solve my original problem. [Sidebar: Interestingly, it has been suggested that it _could_, in future, be possible to do this... (see http://www.mail-archive.com/django-upda...@googlegroups.com/msg45249.html): "The fancy solution would be to loosen this condition slightly and allow an m2m field to be in the fields list (and therefore allow an m2m widget be used) *iff* the through model is just 2 foreign keys with a unique_together pairing (i.e., if the through model is exactly the same as the m2m model that is be automatically generated for the non-explicit through case)."] -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Testing many-to-many models on a legacy database
On Jul 10, 4:40 pm, Rolando Espinoza La Fuente wrote: > On Sat, Jul 10, 2010 at 9:31 AM, Derek wrote: > > Running Django 1.2.1 under Python 2.6. > > I am obviously missing something cruccial, but I am just not sure where this > > is... > > I have a setup which includes a number of many-to-many models in a legacy > > database. For example: > > > class Grouping(models.Model): > > id = models.AutoField(primary_key=True, db_column='GroupingID') > > name = models.CharField(max_length=250, db_column='GroupingName', > > unique=True) > > class TaxAgreement(models.Model): > > id = models.AutoField(primary_key=True, db_column='TaxID') > > title = models.CharField(max_length=100, db_column='TaxTitle', > > unique=True) > > groupings = models.ManyToManyField(Grouping, > > db_table='taxagreementgrouping') > > Use through > keywordhttp://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.mod... > > groupings = models.ManyToManyField(Grouping, through='TaxAgreementGrouping') > Thanks, this fixes those problems. Unfortunately, however, it also introduces a new error. When I try and start the app, I now get: ImproperlyConfigured at /admin/ 'TaxAgreementAdmin.fieldsets[6][1]['fields']' can't include the ManyToManyField field 'groupings' because 'groupings' manually specifies a 'through' model. I need to be able to use the "many to many" box in order to create the associations... Thanks Derek -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Testing many-to-many models on a legacy database
On Sat, Jul 10, 2010 at 9:31 AM, Derek wrote: > Running Django 1.2.1 under Python 2.6. > I am obviously missing something cruccial, but I am just not sure where this > is... > I have a setup which includes a number of many-to-many models in a legacy > database. For example: > > class Grouping(models.Model): > id = models.AutoField(primary_key=True, db_column='GroupingID') > name = models.CharField(max_length=250, db_column='GroupingName', > unique=True) > class TaxAgreement(models.Model): > id = models.AutoField(primary_key=True, db_column='TaxID') > title = models.CharField(max_length=100, db_column='TaxTitle', > unique=True) > groupings = models.ManyToManyField(Grouping, > db_table='taxagreementgrouping') Use through keyword http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through groupings = models.ManyToManyField(Grouping, through='TaxAgreementGrouping') ~Rolando -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Testing many-to-many models on a legacy database
Running Django 1.2.1 under Python 2.6. I am obviously missing something cruccial, but I am just not sure where this is... I have a setup which includes a number of many-to-many models in a legacy database. For example: class Grouping(models.Model): id = models.AutoField(primary_key=True, db_column='GroupingID') name = models.CharField(max_length=250, db_column='GroupingName', unique=True) class TaxAgreement(models.Model): id = models.AutoField(primary_key=True, db_column='TaxID') title = models.CharField(max_length=100, db_column='TaxTitle', unique=True) groupings = models.ManyToManyField(Grouping, db_table='taxagreementgrouping') class Meta: db_table = 'taxagreement' ... class TaxAgreementGrouping(models.Model): #many-to-many id = models.AutoField(primary_key=True, db_column='id') grouping = models.ForeignKey(Grouping, db_column='grouping_id') tax_agreement = models.ForeignKey(TaxAgreement, unique=True, db_column='taxagreement_id') class Meta: db_table = 'taxagreementgrouping' Now when I run the "python manage.py test", the run fails with this error: _mysql_exceptions.OperationalError: (1050, "Table 'taxagreementgrouping' already exists") and if I look through the table creation output statements, I see that 'taxagreementgrouping' table was created along with the 'taxagreement' one. Now if I change the TaxAgreement model to this: class TaxAgreement(models.Model): id = models.AutoField(primary_key=True, db_column='TaxID') title = models.CharField(max_length=100, db_column='TaxTitle', unique=True) groupings = models.ManyToManyField(Grouping) then the test runs just fine. However, when I now want to dump a copy of the database to serve a fixture, I run this: python manage.py dumpdata myapp > myapp/fixtures/myapp.json Error: Unable to serialize database: (1146, "Table 'taxagreement_grouping' doesn't exist") So now I go back to the database, and rename the TaxAgreementGrouping table: RENAME TABLE taxagreementgrouping TO taxagreement_grouping; and also update the model: class TaxAgreementGrouping(models.Model): #many-to-many id = models.AutoField(primary_key=True, db_column='id') grouping = models.ForeignKey(Grouping, db_column='grouping_id') tax_agreement = models.ForeignKey(TaxAgreement, unique=True, db_column='taxagreement_id') class Meta: db_table = 'taxagreement_grouping' Now the dump does not give me the above error anymore, but when I rerun the "python manage.py test", I get: _mysql_exceptions.OperationalError: (1050, "Table 'taxagreement_grouping' already exists") So obviously the problem - and I am not sure exactly what that is? - is not fixed... Any pointers or clues to resolve this? I really need to start testing sooner rather than later. Thanks Derek -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.