Skip to content

Instantly share code, notes, and snippets.

@fchevitarese
Created October 7, 2016 22:33
Show Gist options
  • Save fchevitarese/c8aaab7ccaa390faf8fd476e74cc9c66 to your computer and use it in GitHub Desktop.
Save fchevitarese/c8aaab7ccaa390faf8fd476e74cc9c66 to your computer and use it in GitHub Desktop.
# Modelo A
class Modelo(model.Models):
user = models.ForeignKey('user.User')
# MODELO B
def other_function(instance):
modelos = Modelo.objects.filter(user=instance.client)
print(modelos)
# Aqui vem vazio []
class Client(models.Model):
client = models.ForeignKey('user.User')
saldo = models.IntegerField()
@staticmethod
def notify_client(sender, **kwargs):
# Aqui as rotinas para enviar o email ...
instance = kwargs.get('instance')
other_function(instance) # Esta funçao chama e retorna vazio :(
# Se eu chamar assim, o comportamento é o mesmo.
modelos = instance.client.modelo_set.all()
print(modelos) # [] :(
# Mas se eu faço no console:
usr = User.objects.get(email='fulano@teste.com')
client = Client.objects.get(client=usr)
client.client.modelo_set.all() # Retorna os dados
modelos = Modelo.objects.filter(user=usr) # Retorna os dados
modelos2 = Modelo.objects.filter(user=client.client) # Retorna os dados
Já coloquei o otherfunction como staticmethod do modelo Client mas também não funcionou.
Alguma ideia do que estou fazendo errado?
Desde já agradeço d+!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment