I'd like to build a function in Django that iterates over a set of objects in a queryset and does something based on the value of an arbitrary attribute. The type of the objects is fixed; let's say they're guaranteed to be from the Comment model, which looks like this:
class Comment(models.Model):
name = models.CharField(max_length=255)
text = models.TextField()
email = models.EmailField()
Sometimes I'll want to do run the function over the name
s, but other times the email
s. I'd like to know how to write and call a function that looks like this:
def do_something(attribute, objects):
for object in objects:
# do something with the object based on object.attribute
return results