tro91
tro91

Reputation: 33

Cleaner Python suggestions

Curious if there's a more concise way to set these form values instead of just listing them one by one. The venue_items dictionary doesn't contain any extra (key, value) pairs that aren't declared here if that makes any difference.

    form.name.data = venue_items["name"]
    form.city.data = venue_items["city"]
    form.state.data = venue_items["state"]
    form.address.data = venue_items["address"]
    form.phone.data = venue_items["phone"]
    form.image_link.data = venue_items["image_link"]
    form.facebook_link.data = venue_items["facebook_link"]
    form.genres.data = venue_items["genres"]
    form.website.data = venue_items["website"]
    form.seeking_talent.data = venue_items["seeking_talent"]
    form.seeking_description.data = venue_items["seeking_description"]

Upvotes: 0

Views: 49

Answers (1)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96236

I mean, there is a way to do this dynamically, something like:

for key, value in venue_items.items():
    getattr(form, key).data = value

Or if your dict contains extra key-value pairs that wouldn't be used, something like:

keys = ["name", "city", "state", "address", "phone", "image_link", "facebook_link",
        "genres", "website", "seeking_talent",  "seeking_description"]

for key in keys:
    getattr(form, key).data = venue_items[key]

Upvotes: 2

Related Questions