Wednesday, December 1, 2010

Re: Problem with ForeignKey to an abstract class



On Wed, Dec 1, 2010 at 12:48 PM, Ferran <ferran@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=[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')


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