I’m new to python, kindly i need help. I’m trying to update in an API, first am query to get the row i want update, i don’t want to change all the attributes in the row just some. I have tried to use setattr() it is setting the new attributes with values but it is trying to replace as the results am running into integrity error. Below is my code.
def update_town(session, town_dict):
town = session.query(Town).where(Town.id == town_dict[“id”] ).one()
#setting new values
for key, value in town_dict.items():
town_attribute = setattr(town, key, value)
town_attribute = value
below is my route
@app.route(’/town’, methods = [‘POST’, ‘GET’, ‘PATCH’, ‘PUT’,‘DELETE’])
defining the function without pasing arguments
if request.method == ‘POST’:
if request.method == 'GET':
return getTown(session, request.json)
if request.method == 'DELETE':
return delete_town(session, request.json)
if request.method == 'PUT':
return update_town(session, request.json)
Just a suggestion, but you may want to edit your post to make it easier for people to read your code without too much effort. In my experience (which is in no way limited to this forum) I’ve found that the easier it is for others to read what one has posted, the better the chances of getting some feedback.
You understand how markdown works, right? If not I can post a link to a guide.
I hope you don’t think that I’m being patronising (I can assure you that is not my intention), it’s just that your post is not easy to read, as is.
I see , no thank you …I understand… I’ll try to do so
You need three backticks for the “code fence” when you post, you only have one.
code goes here
A single backtick ` is only useful for inline code, like
this. For multiple lines, you need to use three.
Unfortunately there is not enough information to answer your question.
At the minimum, we will need to know which API or framework you are using, and what error you get. Then you need to hope there is somebody here who knows that API and can answer your question.