0

I have the model for a team object. I want to add objects in this models by parsing. So, I have parsing code in the views.py. How can I do adding of that data in the model in the views.py. Should I use queryset or something like that?

As I know there is approach to add objects using Django shell. But is it possible to add objects automatically? While the project is deployed on a hosting it is not possible to use Django shell. I tried to use manual adding using lists and dictionaries as the variable during template rendering.

from django.shortcuts import render
from django.views.generic import TemplateView
from bs4 import BeautifulSoup
from urllib.request import urlopen


def homePage(request):
    tn = []
    ti = []
    for team in teamList:
        tn.append({'name': team})

    for img in imgList:
        tn.append({'img': img})

    teamDict = {
        'team': tn,
        'matchLink': linkSrc,
        'img': tn,
    }
    return render(request, 'home.html', teamDict)


# Parser
try:
    html = urlopen("//some url//")
except:
    print("Connection problems")


teamList = []
imgList = []
bsObj = BeautifulSoup(html, "lxml")
futMatches = bsObj.find("div", {"class": "esport-match-future-list"})
for link in futMatches.find_all("div", {"class": "esport-match-single"}):
    linkTag = link.find("a", {"class": "team-vs-team"})
    if "tbd" not in linkTag.attrs["href"]:
        teamDiv = linkTag.find_all("div", {"class": "team"})
        for item in teamDiv:
            name = item.find("span", {"class": "name"}).text
            photo1 = item.find_all("img")
            for img in photo1:
                imgSrc = "https://dota2.ru" + img.attrs["src"]
                imgList.append({"img": imgSrc})
            teamList.append({"name": name})
        linkSrc = "https://dota2.ru" + linkTag.attrs["href"]  # Match link



# End of parser



from django.db import models

class Match(models.Model):
    team1 = models.CharField(max_length=30, default='Team 1')
    team2 = models.CharField(max_length=30, default='Team 2')
    #matchTime = models.DateTimeField()
    tour = models.CharField(max_length=50, default='Tournament')

class Team(models.Model):
    name = models.CharField(max_length=30, default='Team')
    urlslug = models.SlugField(max_length=5)

    #Player 1 info
    player1Name = models.CharField(max_length=50, default='Player')
    player1Photo = models.ImageField(upload_to='')
    player1Nation = models.ImageField(upload_to='')

    #Player 2 info
    player2Name = models.CharField(max_length=50, default='Player')
    player2Photo = models.ImageField(upload_to='')
    player2Nation = models.ImageField(upload_to='')

    #Player 3 info
    player3Name = models.CharField(max_length=50, default='Player')
    player3Photo = models.ImageField(upload_to='')
    player3Nation = models.ImageField(upload_to='')

    #Player 4 info
    player4Name = models.CharField(max_length=50, default='Player')
    player4Photo = models.ImageField(upload_to='')
    player4Nation = models.ImageField(upload_to='')

    #Player 5 info
    player5Name = models.CharField(max_length=50, default='Player')
    player5Photo = models.ImageField(upload_to='')
    player5Nation = models.ImageField(upload_to='')

    #Standin 1 info
    standin1Name = models.CharField(max_length=50, default='Standin', null=True)
    standin1Photo = models.ImageField(upload_to='', null=True)
    standin1ation = models.ImageField(upload_to='', null=True)

    #Standin 2 info
    standin2Name = models.CharField(max_length=50, default='Standin', null=True)
    standin2Photo = models.ImageField(upload_to='', null=True)
    standin2Nation = models.ImageField(upload_to='', null=True)

I want that after the parsing, the data was added to model as new object and finally use the model during the rendering of the template.

1 Answer 1

1

If you want to add players and teams automatically from a third-party api, you'll need to call the api, parse the results and then update the model. For example, here's a pseudo-code example:

def update_team_name(team):

  ...your parsing code here...     

  Team.objects.filter(pk=team.pk).update(name=team.name)
  team.save()
  return True

BTW, I would consider restructuring your model: add a Player model. Enhance the team so it includes players and a stand-in indicator.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.