1

I'm using some javascript libraries that won't let me pass in self.full_name created using def full_name(self):

Wondering how I should go about updating a full_name field based on changes to (or creation of) any of the 3 name fields.

class Employee(models.Model):
  first_name = StringProperty(max_length=25)
  middle_name = StringProperty(max_length=25)
  last_name = StringProperty(max_length=50)

  full_name = StringProperty(max_length=100)
  # lots of Icelanders have the same first and last name...
  full_name_includes_middle_name = BooleanProperty(default=False)

Right now I'm looking into @receiver and created or update_fields is looking promising...

@receiver(post_save, sender=Employee)
def update_full_name(sender, update_fields, created, instance, **kwargs):
  if instance.middle_name_is_part_of_full_name == True:
    if created or update_fields is 'first_name' or 'middle_name' or 'last_name':
      instance.full_name = instance.first_name + " " + instance.middle_name + " " + instance.last_name
      instance.save()
  else:
    if created or update_fields is 'first_name' or 'last_name':
      self.full_name = self.first_name + " " + self.last_name
      instance.save()

^ but this gives the error:

update_full_name() missing 1 required positional argument: 'update_fields'
Kermit
  • 4,922
  • 4
  • 42
  • 74

1 Answers1

1

In this case there is no diff between create or update operation.

You can try this:

@receiver(pre_save, sender=Employee)
def update_full_name(sender, instance, **kwargs):
  if instance.middle_name_is_part_of_full_name == True:
      instance.full_name = f"{instance.first_name} {instance.middle_name} {instance.last_name}"
  else:
      instance.full_name = f"{instance.first_name} {instance.last_name}"
Kermit
  • 4,922
  • 4
  • 42
  • 74
Youssef BH
  • 534
  • 5
  • 14
  • thanks so much =) im trying it out now. `hooked() got an unexpected keyword argument 'update_fields'` – Kermit Jun 27 '18 at 23:54
  • looks like this error is happening right after `form.is_valid: form.save` i'll fix it – Kermit Jun 27 '18 at 23:58
  • Yes. `line 206, in save super(Profile, self).save(*args, **kwargs) TypeError: hooked() got an unexpected keyword argument 'update_fields'` – Kermit Jun 28 '18 at 00:01
  • This is weird, ok try without `update_fields`, I updated the answer. – Youssef BH Jun 28 '18 at 00:07
  • new error. maybe it has to do with calling save from post_save? `encoding with 'idna' codec failed (RecursionError: maximum recursion depth exceeded)` – Kermit Jun 28 '18 at 00:11
  • https://stackoverflow.com/questions/39481625/how-can-i-prevent-post-save-recursion-in-django – Kermit Jun 28 '18 at 00:13
  • good post, yep you're right. we're calling save from post_save. so try `post_save.disconnect(update_full_name, sender=sender)`. Check out the answer, i guess this time it should work. – Youssef BH Jun 28 '18 at 00:18
  • Thanks so much for your help! Also f"" where f goes outside the quotes ``` @receiver(pre_save, sender=Profile) def update_full_name(sender, instance, **kwargs): if instance.middle_name_is_part_of_full_name == True: instance.full_name = f"{instance.first_name} {instance.middle_name} {instance.last_name}" else: instance.full_name = f"{instance.first_name} {instance.last_name}" ``` – Kermit Jun 28 '18 at 00:23