foreign keys - username search in Django -
i want search username
, way code set can search id
in user model. how can search username
? can't difficult, i'm failing see how done.
model
class incident(models.model): incident_id = models.charfield(max_length=1000) user_id = models.foreignkey(user)
form
<form method='get' action='/search_incidents/'> <div class="row"> <div class="form-group col-md-3"> <label>incident id</label> <input type="text" name="incident_id" value="{{ incident_id_query }}" class="form-control"/> </div> <div class="form-group col-md-3"> <label>user id</label> <input type="text" name="user_id" value="{{ user_id_query }}" class="form-control"/> </div> </div> </form>
view
@login_required def search_incidents(request): if request.method == 'get': incident_id_query = request.get.get('incident_id') user_id_query = request.get.get('user_id') try: incident_id = str(incident_id_query) except valueerror: incident_id = none try: user_id = str(user_id_query) except valueerror: user_id = none list = [incident_id,user_id] if all(x none x in list): #check if `list` contains none incident_list = none #this in case query comes empty else: #perform query incident_list = incident.objects.filter(incident_id=incident_id).filter(user_id=user_id) return render(request, 'search_incidents.html', { 'incident_list' : incident_list })
Comments
Post a Comment