Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Dec 6th, 2018 56 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. class Book(models.Model):
  2.     num_pages = ...
  3.     author = ...
  4.     date = ...
  5.    
  6. d = {"num_pages":40, author:"Jack", date:"3324"}
  7.    
  8. Book.objects.create(**d)
  9.    
  10. Book.objects.filter(pk=pk).update(**d)
  11.    
  12. Book.objects.create(**d)
  13.    
  14. instance, created = Book.objects.get_or_create(slug=slug, defaults=d)
  15. if not created:
  16.     for attr, value in d.iteritems():
  17.         setattr(instance, attr, value)
  18.     instance.save()
  19.    
  20. Book.objects.filter(id=id).update()
  21.    
  22. for attr, value in validated_data.items():
  23.     setattr(instance, attr, value)
  24. instance.save()
  25.    
  26. def is_simple_editable_field(field):
  27.     return field.editable and not field.primary_key and not isinstance(field, (ForeignObjectRel, RelatedField))
  28.  
  29. def update_from_dict(instance, attrs, commit):
  30.     _allowed_field_names = {
  31.         f.name for f in instance._meta.get_fields()
  32.         if is_simple_editable_field(f)
  33.     }
  34.  
  35.     for attr, val in attrs.items():
  36.         if attr in _allowed_field_names:
  37.             setattr(instance, attr, val)
  38.  
  39.     if commit:
  40.         instance.save()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top