[Answered ]-Access table in sqlite using django and query creation

1👍

I can’t figure out in the line user = User.objects.get(id = user_id) where user_id is coming from. Is it coming from reader or can you use user = request.user?

If my assumptions are correct, then I think you can first get the User, UserAcoount and Localization tables first before the for loop:

@login_required(login_url='login') 
def uploadCoord_file_view(request):
    
    # Get the user
    user = request.user

    # Access the Localization table of the specific user
    localization = Localization.objects.get(user=request.user)

    # Access the UserAccount (you did not provide that model, so I am guessing
    user_account = UserAccount.objects.get(user=request.user)

   ...

Then you can do the matching and updating like this:

###### Here I need to access the table Localization and create the query

# Filter all Coordinates that have the same date and user as the Localization table
coordinates = Coordinates.objects.filter(date=localization.date, user=user_account)

# Update the values of all the matches
coordinates.update(latitude='North', longitude='West')

It is best to put all get queries in a try/except statement, otherwise you’ll get an error if the get returns no matches:

try:
    # Access the Localization table of the specific user
    localization = Localization.objects.get(user=request.user)
except Localization.DoesNotExist
    # Code to handle what should happen if no match exists

Leave a comment