Wednesday, December 1, 2010

Problema con ForeignKeys hacia una tabla abstracta

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Buenas tardes a todos;

Quiero tener ForeignKeys hacia el modelo padre en las tablas intermedias
(manytomany trough) para tener entradas únicas en las mismas.

El problema es que quiero que este modelo padre sea abstract, ya que
nunca querré instanciarlo, utilizando DomainOwned o DomainPending en vez
de esta.

El error que tengo es este:

File "/home/ferran/intranet/domains/models.py", line 48,
in ContactType
domain = models.ForeignKey(DomainUbi)
File
"/usr/lib/python2.7/site-packages/django/db/models/fields/related.py",
line 808, in __init__
assert not to._meta.abstract, "%s cannot define a relation with
abstract class %s" % (self.__class__.__name__, to._meta.object_name)
AssertionError: ForeignKey cannot define a relation with abstract
class DomainUbi

En las foreignkeys a DomainUbi, tanto en ContactType como en
AssociatedNameServer.

Había pensado que podría implementar los fields ns y contacts en ambas
tables (DomainOwned y DomainPending), utiliando generic relationships,
pero no me parece correcto.

Alguien me puede echar una mano?
- ---

class DomainMinimal(models.Model):
sld = models.CharField(
max_length=63
)
tld = models.ForeignKey(common.Tld)
name = property('_domain_name')

def _domain_name(self):
return u'%s.%s' % (self.sld, self.tld)

def __unicode__(self):
return self.name

class Meta:
abstract = True


class DomainUbi(DomainMinimal):
owner = models.ForeignKey(contacts.Contact, related_name='Owner')
contacts = models.ManyToManyField(contacts.Contact,
through='ContactType')
ns = models.ManyToManyField('NameServer',
through='AssociatedNameServer')
period = models.IntegerField(validators=[MaxValueValidator(10)])

class Meta:
abstract = True

class DomainOwned(DomainUbi):
crdate = models.DateField()
exdate = models.DateField()

class DomainPending(DomainUbi):
status = None # just an example, now


class ContactType(models.Model):
contact = models.ForeignKey(contacts.Contact)
domain = models.ForeignKey(DomainUbi)
role = models.CharField(
max_length=10,
choices = (
('admin', 'Administratiu'),
('billing', 'Facturacio'),
('tech', 'Tecnic'),
)
)
class Meta:
unique_together = ('contact', 'domain', 'role')

class AssociatedNameServer(models.Model):
ns = models.ForeignKey('NameServer')
domain = models.ForeignKey(DomainUbi)
priority = models.IntegerField(
validators=[MaxValueValidator(4)]
)
def __unicode__(self):
return "%s (%s)" % (self.ns.host, self.ns.ip)

class Meta:
unique_together = ('ns', 'domain', 'priority')

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBCAAGBQJM9jhAAAoJEAWOKfEESaOwaOgP/36oNmwpYoYkqhuYYuR53Vge
YnIAnMoEIJIze9V4zJhDDWGcvRy8cjz86oqdLQ6Qg3BA+1JxrYML3GY4BWAAslrB
SAPKNfSjP5zspNdfEHrmnq1/bEQnqeeRor5AmfyXUhMBWdE26cCiMWgZVE+M6L6i
+jMku/50COW4JUk55mOE54KdicQ5ChCCWBv1G3aP9mBoMkKQLkLAvTK4DY+bv2xm
vWxXdyjKTGjF5Ck8JSXtq4N7rJev68eoTaK+lc392vkqTrQOrOgDan60hoiej8Dk
cr0ITh9fQuIV59tbL2Bf8vYK8OR3yWoVdL+lfw6xbdovq4qVpjDj01ZN3a42+Mvx
0HFJfnGUhu6zlK1LZWAayIGS+bDnwbvIDkVcx5GxnmQYM9jomMojiirhGttx6mlc
zMPXRzFAw2YyWtFYpSn2bA4e42G83sssRqaQlVdNeGFzP6I4Y67I5TDrPgd1tTKJ
QyC1eFNRxhfoRcYVfYMoTSFBd2FLpLPzMb3cNm3GOeasEgxlj3p1nBErljCdBl2w
jyMLE4/bov63szC78iThMuyV+AxO8ENmLe+NRkHMMToHp6eoyikZR9jRhcAJN0nG
UoQObbeRtNBeko63qwykWOHJgZ+zrkRIk2X3rfbHiW0zXcsWXXBReE+eO7mWIbdY
CrByJHKi89DUea6mUtQ4
=S6Gj
-----END PGP SIGNATURE-----

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment