Problem with ForeignKey to an abstract class

224 views
Skip to first unread message

Ferran

unread,
Dec 1, 2010, 6:48:07 AM12/1/10
to django...@googlegroups.com

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

Hello everyone,

I want to have foreign keys to parent model on a manytomany
intermediate table (through) to have unique entries for that domain.

The problem is i want this parent model class to be an abstract one,
because i don't ever want to instantiate it, using DomainOwned or
DomainPending instead.

The error i'm getting is this:

File "/home/ferran/0_ubilibet/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

On both the foreignkeys to DomainUbi on models ContactType and
AssociatedNameServer.

I've thought maybe i can implement contacts and ns fields in both
DomainOwned and DomainPending, using generic relationships, but it
does not seems right to me.

Can anyone help me on this, please?

- ---

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/

iQIcBAEBCAAGBQJM9jX2AAoJEAWOKfEESaOw1xIP/2ijO1VOXmnxoT1VdiuilcgP
qK+9YXS7Y6bwIhy9zGHF5QB/wAVQyYbYEA/mpLPP09ShA7LV2qO5ZRsdR0TYChfF
P/mVoz73BSkFQLw+B4rS2XCTfXCzumFEslrqWtSAyNnxU1+LjtT/GpFfxldU3PiF
oNnNbAJSjLdd1OvtIP7I3iquj+AV5iuzSDmi38zL5xT9mF75+hg0TXkcA04KMNKJ
AfvZ3j5ofRHJCwpu1jYtE85OzXWasffJeYvXiPLHGHb/Pdp0WwvFCBTb9nIRwVVR
mt/d5lIp6ATuakCxMz9TDM3wf0+81ynGI9s/csZuY3c0RAlKpkzgYMYARC81haiQ
mKJJgQ9J9IHcry9XcMcOZ+x4j+c5JYvNQx5YI+LAuLdXGqzlmQ9mz3Ur0Jsu9H5I
b1wOSiIqDF6uITjCjqlQL1zrSgPSD1NKSBzSm/LO3bBjEqqZL31K7NNUtN70GGhS
Qb85TxpitLHMBhsPjQhi6248CoL5s3/MkPHC9JHajPa2G6ZT+gR7tCiXnkmURdU6
UO1YelGx751GUTRZQxhBPorNHgMPtYrZ4+fPfGVxWfjG1vXS9WXgs4O+xYaIpnMK
lCmX431JJstDHFERbc48qvnuyvy0QX4J1iRsbVJJ8yjYG8vSRzKSFMksibH+PCHl
5UipC5zZfUyxNWR8/bDS
=nHpW
-----END PGP SIGNATURE-----

Ferran

unread,
Dec 1, 2010, 11:25:44 AM12/1/10
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 01/12/10 12:48, Ferran wrote:
> I've thought maybe i can implement contacts and ns fields in both
> DomainOwned and DomainPending, using generic relationships, but it
> does not seems right to me.

Shame on me.

This is the right way to do it, the only way, in fact.

I really want django to solve it with his own magic, but... no.

My solution:


# class DomainUbi

change:

contacts = models.ManyToManyField(
contacts.Contact,
through='ContactType'
)

to:

contacts = GenericRelation('ContactType')

# class ContactType

change:
domain = models.ForeignKey(DomainUbi)
to:
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey()


change:


unique_together = ('contact', 'domain', 'role')

to:
unique_together = ('contact', 'role', 'content_type', 'object_id')

Thank you!


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

iQIcBAEBCAAGBQJM9ncHAAoJEAWOKfEESaOwVYQP/1V6w8UZn1NkLWS3YC49QDkC
QRlLpzUNH13pkYOXYF+2kEL6x1ZGCyL2G33R86485Ea/KP8kLo0TJT+nEn2sjFaW
0adG13w6yoXsE0Khot9vqDwKzqBKhpt2SMXxs4nCGjE66D1bUjRutq/xMgIY8EM+
6DJr1B7u1/n5jH/TKOOmfn3xcpo16Fe3rYc5udVoxa6tWdbMHIakozI2pkec+AMU
kDOlQg3OROCSSzurcjCybN4YNRW6bEM+9auh/jPaRXTry9vfy7wZyCn0Oije9knL
8l0v2JUZbmZTlZpFTX+oM/sziKN8s6EhgDWBH3Ii3WwapMFcEMa9vo8Ejb7SQUPL
HlfCxjjkeOHP07Eb/OkpWO7uACA2SF2Z4XLLG3+kxT9fP6trwRa2N0/8pZ+JFDh2
/ujTtGCGQIfG5dbjXB3sZfmT6h191MKMhKSpxmGfpH1JqFVQ6NIc7Kd3OwnKYi99
bYlFb3O1j2MwslTxrMxhhMOizdo0QEIbUYIdHCYXyes4ibecD7ZufT2tF6D/Y4P+
BLzj5ASqyRrnfWHNq1NRjLHm0CT27iXhMjn4AQjXae9vp1gUNMwA6d5h0LQmahTG
2bQ8VOxWvHwbjbJpTjHIcGt4396hkTa/Zcca4JuD5MwdHGvc8tqgpM4JYv9vw4m4
JM8ncLHB8bem+UoiquGk
=BCc+
-----END PGP SIGNATURE-----

Marc Aymerich

unread,
Dec 1, 2010, 11:52:31 AM12/1/10
to django...@googlegroups.com
Sorry, but I don't understand at all what you're trying to do. Can you elaborate a little more? And why you're doing a FK to an abstract class like 
domain = models.ForeignKey(DomainUbi) ? 
 
--
Marc
Reply all
Reply to author
Forward
0 new messages