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/
iQIcBAEBCAAGBQJM9jieAAoJEAWOKfEESaOwRzkQAMQivnnLkrZyEsMCpJrEzVLo
j9egZmRjussczVqXiBebZkO29Y0OR0C4l28lV2ji2F5+JVyxoc0wFxc2NASW87C6
m9RDVCHHj8PY5Uh6mvMjmBtdsKSh1ZGvCzp9b6sLO/WPh0SdclXjXVmMAwATAr/g
EoKHARrLFAOkPoFfZiKUMfiiRC7vdIvsH7U3kK929dTqq9T82wD03RXGwrocyhvU
5lXchgt/fLdLFfLD1nQnhkC01AztgrFznapvrnhG/757ThcC41wQCMfhiikNFFYD
sJwnK19zkSnIaIo3ga7fjypZ9QzhjT35y542hdZgy6SX5+oilVbMnnAoFMgTAJWb
WDlBIfGw6FJiM6+hzEQhsRZdJnvVhssrt9C6C81aR+gnjZbVGAaFRHTTYZmekiBZ
KKv7vjJrAzB1JxoqYIk0IqTvJjnYsODkKs3FnHXQaiXckQ75vsF0Dbimgi2Xrhe8
7wjHv/JOjAlCqBv7isqGiV9PWZ03s87WZU1fvWvpMGALshFheuVdhdeZQW+Nlewk
GoW+IhO0oPQ9BVALP6lzt1JBHDWgl3CE6/cxS20BvBhMPE63AcepPnDX/xQtlXJ7
yXdKMLwfRrROHcH+4dT07KvQ3fz5xkUWHNBjW2vCfw/wPZ1tFxOeCHVFhkRZ4m3i
wQU/HMvd0zEWiL/vi83H
=wOqe
-----END PGP SIGNATURE-----
Alguien sabe si hay algún snippet o plugin que permita compartir los las
entradas con distintas redes sociales...
Salu2
Roylan
On 01/12/10 15:42, Marc Aymerich wrote:
>
> no puedes hacer eso con una clase abstracta
Muchas gracias por la respuesta, Marc.
Según voy viendo, a una clase abstracta no se la puede referenciar.
Por lo tanto, parecer ser que no es posible tener una relación
ManyToMany hacia otra tabla usando una tabla intermedia.
¿Es posible hacerlo de otra manera?
Muchas gracias de antemano
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iQIcBAEBCAAGBQJM9m55AAoJEAWOKfEESaOwQm4P/iSljjkvMWNMvO80apRtd64+
E4pIyKoPwozeDnX3/zz5B03f7qJ9oFV35oiYKUaM91BHf6fMAYZzFjaHZp3keoWW
hMS5BiaBNR9Gh8sgzeWiirPWFhyDyEfltN5iOSguNq1XMBX0chijfkukbdP2x6vE
oekGoez+/NoCq1TCelPrdP5wesyZd9lb8uBGTDNK27igWnZRhQxPYKfv2rfLgcbq
/XQQ4FEg0iFtZBB6WFeY3XQoftIrv3g7jiTunh2I32YeMJkMloCGuRj4kxP4hbDN
q4mLJyHoyVC0yK9GO39dnhWsBxC0VgWC8HQrcD+nzWxED7a1vuUsouPbLQNujFl7
FLjjNBfHUOjmQnnhVJChhpkoLh/WA+gInmzUfxRMeDpal0peC3ELY+B7hAdIWFxc
S3Lmg1AlDR6obYmGvc7KZGWYHq2MS8Z0cfEBdqLX5qCinQJZAbMt4FOxKSBfh6Ef
0e2Z0J3u19VqeHozQ8iuEjlUq4VpgZrqhuNEnzKG+qK4skVwGB+dCB5LdZuOtKey
dzGX4JIXDGJh4CTO5bHiX7ywGPrZpd4ptBjCsOjUhvRUI1PA4cG+TKo3qQSZBgf4
LXesYFrRRDN/JmQUuuIbSgGAqWh7cZK7UxtWzP8hRpC21DZmIQGMQEY3J6hEh/g0
pxoP0pUllKrjWM+qXiXe
=KH5U
-----END PGP SIGNATURE-----
if (message.sender != user) and (message.recipient != user):
Si el emisor o el destinatario no son iguales al usuarios que está
activo, deberia de lanzar una página 404, y el problema es que cuando
intento leer mis mensajes también lanza una página 404...
Para comprobar estos datos inserté:
print message.sender
print message.recipient
print user.username
antes de:
if (message.sender != user) and (message.recipient != user):
y en este caso devolvió:
[06/Dec/2010 12:15:31] "GET /jc/linux/mensajes/view/79/ HTTP/1.1" 404
roylan
roylan
roylan
[06/Dec/2010 12:15:31] "GET /jc/linux/mensajes/view/79/ HTTP/1.1" 404
cuando intento leer un mensaje que me envié a mi mismo...
Ya he realizado tantas pruebas que decidí enviar el problema a la
lista...
Salu2
def view(request, message_id,
template_name='django_messages/view.html'):
"""
Shows a single message.``message_id`` argument is required.
The user is only allowed to see the message, if he is either
the sender or the recipient. If the user is not allowed a 404
is raised.
If the user is the recipient and the message is unread
``read_at`` is set to the current datetime.
"""
user = request.user
now = datetime.datetime.now()
message = get_object_or_404(Message, id=message_id)
if (message.sender != user) and (message.recipient != user):
raise Http404
if message.read_at is None and message.recipient == user:
message.read_at = now
message.save()
return render_to_response(template_name, {
'message': message,
}, context_instance=RequestContext(request))
view = login_required(view)
Si el problema es este, quizás te vendría bien echar un repaso a conceptos básicos de orientación a objetos. Una opción es el capítulo de «Orientación a Objetos» del manual de Python de Mundo Geek, http://mundogeek.net/tutorial-python/.if (message.sender != user.username) and (message.recipient != user.username):
Sergio Sánchez Ramírez · Desarrollo web +34 609 267 872 · +34 922 080 258