i am trying to get date from a user when sending a webhook. The JSON will be handling the username of which user i want the data to be used from . but i get an error that user is expecting an id how can i make it that it is looking for a username rather then id.This is the error
and this is my Models code
class Bybitapidatas(models.Model):
#user = models.OneToOneField(User, null=True,on_delete=models.CASCADE)
user = models.ForeignKey(settings.AUTH_USER_MODEL,default=1,on_delete=models.CASCADE)
apikey = models.CharField(max_length=30)
apisecret = models.CharField(max_length=40)
def __str__(self):
return str(self.user) self.apikey self.apisecret
and this is my views file where i am trying to get the data for the user
@csrf_exempt
@require_POST
def webhook(request):
#get current users keys
jsondata = request.body
data = json.loads(jsondata)
for the_data in data:
users = the_data
print(users)
database = Bybitapidatas.objects.get(user=users)# this is where it is asking for the id?
for apikey in database:
apikey = apikey.apikey
for apisecret in database:
apisecret = apisecret.apisecret
session = HTTP(endpoint='https://api-testnet.bybit.com/', api_key=apikey, api_secret=apisecret,spot=True)
print(session.place_active_order(
symbol="BTCUSDT",
side="Buy",
type="MARKET",
qty="20",
timeInForce="GTC"
))
print(apikey)
CodePudding user response:
Use __
to follow the user relationship in your query to filter by the username field of the related user
database = Bybitapidatas.objects.get(user__username=users)
Bybitapidatas.user
is not unique, this query may return multiple results in which case you will get an error.