Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
calling Rest Api from Django admin Panel

My model has 2 values.One is URL and other one is price.When creating a new object via django admin panel.I want when i input URL in input field,a request should be sent to that specific URL and fetched the updated price for the field and populate it.I have gone through documentation and helping material but did not find help.Any help in this matter is highly appreciated. Thanks

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can do something like this:

 import requests
 import json


class Model(model.Model):
    url=models.URLField()
    price=models.FloatField()

    def save(self, *args, **kwargs):
        resp = request.gets(self.url)
        #assuming that json response looks like this: {'price': 2000} 
        resp_str = resp.content.decode()
        resp_dict = json.loads(resp_str)
        self.price = resp_dict.get('price')
        super().save(*args, **kwargs)

note: The code is not tested, but it is enough to give a general idea to proceed further.

over 4 years ago · Santiago Trujillo Relatório

0

I was struglign with this for so long and finally I accomplished it. What I did is

models.py
class Testone(models.Model):
    url = models.URLField()
    price = models.DecimalField(max_digits=30,decimal_places=5)

admin.py
@admin.register(Testone)

class PriceAdmin(admin.ModelAdmin):
    fields = ['url']
    def save_model(self, request, obj, form, change):
        obj.price = poang.ikea_ru(request.POST['url'])['price'] # calls external function w/ the requests data. 
#function returns dict that's why i use ['price'] to get a value from the dict
        super(PriceAdmin,self).save_model(request, obj, form, change)

I hope that is still helpful, I struggled with this for a little while.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda