Wednesday, May 2, 2018

Re: Admin site and ModelAdmin

On 3/05/2018 10:33 AM, Mike Dewhirst wrote:
> On 3/05/2018 7:00 AM, jt.oldnews@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(Employee, self).save(*args, **kwargs)

Sorry - should have read what I pasted in

> 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+unsubscribe@googlegroups.com
>> <mailto:django-users+unsubscribe@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+unsubscribe@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/88249111-78d2-5076-0ea9-b0cb5d5d5ae6%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment