Hi all, I have a class in which I want to override the get_or_create method. Basically if my class doesn't store the answer I want it do some process to get the answer and it's not provided. The method is really a get_or_retrieve method. I have heavily borrowed this from db/models/ query.py and I can't figure out the line "obj = self.model(**params)". I don't understand what the attr model needs to be and it's not intuitively obvious what this should be. Even looking back at the query.py I can't figure this out. Can someone explain this to me? I would really like to understand it and fix my code.
So here's the class: class P4User(models.Model): user = models.CharField(max_length=100, primary_key=True) fullname = models.CharField(max_length=256) email = models.EmailField() access = models.DateField(auto_now_add=True) update = models.DateField(auto_now_add=True) @classmethod def get_or_retrieve(self, username, auto_now_add=False): try: return self.get(user=username), False except self.model.DoesNotExist: import P4 import datetime from django.db import connection, transaction, IntegrityError p4 = P4.P4().connect() kwargs = p4.run(("user", "-o", username))[0] p4.disconnect() params = dict( [(k.lower(),v) for k, v in kwargs.items ()]) obj = self.model(**params) sid = transaction.savepoint() obj.save(force_insert=True) transaction.savepoint_commit(sid) return obj, True except IntegrityError, e: transaction.savepoint_rollback(sid) try: return self.get(**kwargs), False except self.model.DoesNotExist: raise e def __unicode__(self): return str(self.user) I am able to get the params but I haven't defined self.model. I don't understand what that needs to be and it's not intuitively obvious what value that should be. Even looking back at the query.py I can't figure this out. Can someone explain this to me? I would really like to understand it and fix my code. Thanks --~--~---------~--~----~------------~-------~--~----~ 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+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---