django - How to iterate over object attributes and check for duplicate in python? -


i have list of objects stored in variable. each object has 2 attributes: temp_name , temp_body. now, want iterate through list of objects , see if name given user exists in temp_name object. tried doing this:

temp_name = request.post.get('temp_name') templates = emailtemplate.objects.all()          template in templates:             if template.temp_name == temp_name:                 data = {'status': 'exists'}                 return jsonresponse(data)             else:                 data = {'status': 'exist_not'}                 return jsonresponse(data) 

but works first item in list, won't work second item onwards. how working?

the code returning early. need move else part out of loop iterate templates:

for template in templates:     if template.temp_name == temp_name:         data = {'status': 'exists'}         return jsonresponse(data) data = {'status': 'exist_not'} return jsonresponse(data) 

btw, don't need use loop. use queryset.filter check whether there matching template exist:

temp_name = request.post.get('temp_name') exists = emailtemplate.objects.filter(temp_name=temp_name).exists() data = {'status': 'exists' if exists else 'exist_not'} return jsonresponse(data) 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -