Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Problem with ForeignKey to an abstract class [RSS Feed]

#1 Dec. 1, 2010 11:49:11

F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem with ForeignKey to an abstract class


-----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=)

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=
)
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-----

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 1, 2010 16:26:46

F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem with ForeignKey to an abstract class


-----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-----

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 1, 2010 16:52:58

Marc A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Problem with ForeignKey to an abstract class


On Wed, Dec 1, 2010 at 12:48 PM, Ferran <fer...@fompi.net> wrote:

>
> -----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=)
>
> 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=
> )
> def __unicode__(self):
> return "%s (%s)" % (self.ns.host, self.ns.ip)
>
> class Meta:
> unique_together = ('ns', 'domain', 'priority')
>


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

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Problem with ForeignKey to an abstract class [RSS Feed]

Board footer

Moderator control

Enjoy the 21st of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support