On 3/05/2018 7:00 AM, jt.oldn...@gmail.com wrote:
I'm new to Django and web development in general (C++ background) and I've got amazingly far very quickly, but there's one thing I've been struggling with for a couple days now :-(. Forgive me if I screw up the lingo.

I've got a ModelAdmin page for registrants of a conference which has fields for Employee ID, Name, email etc. The registrant does not have to be an employee, but if they are I could lookup their name, email etc. from an external source (already have the Python code for this). I'd like to have a button next to the employee id field which would lookup the employee's details and fill in the appropriate fields. This seems to me that it shouldn't be too hard but I am spinning my wheels here.  Do I need to ditch ModelAdmin?  Can someone please point me in the right direction.

You may not need a button which, in the Admin, implies javascript and an API. In your model you can intercept the save() method to visit another database/system to fetch the detail before the record is saved. There are probably many ways to do this. One uses a pre-save signal [1] but I prefer to override save() [2].

class Employee(models.Model):

    ... all the fields etc

    fetchdata = models.BooleanField(default=False, blank=True)

    def save(self, *args, **kwargs):
        if self.fetchdata:
            self.fetchdata = False
            try:
                self.this, self.that = self.fetch_data()
            except Exception:
                # avoid losing other changes
                pass
        super(Substance, self).save(*args, **kwargs)

    def fetch_data(self):
        #call external database with employee ID and return necessary detail
        return this, that

[1] https://docs.djangoproject.com/en/1.11/ref/signals/#pre-save
[2] https://docs.djangoproject.com/en/2.0/ref/models/instances/#saving-objects

hth


Thanks,
JT
--
You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com <mailto:django-users+unsubscr...@googlegroups.com>. To post to this group, send email to django-users@googlegroups.com <mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1324e38c-ed15-4f3c-8e1e-cd7df220b96e%40googlegroups.com <https://groups.google.com/d/msgid/django-users/1324e38c-ed15-4f3c-8e1e-cd7df220b96e%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/add76f8e-b999-e9df-be5f-01bd2de26c3f%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to