import xml.etree.ElementTree as ET
tree = ET.fromstring("xml_string") # or ET.parse("path_to_xml_file")
for element in tree.findall("globalVariables/globalVariable"): # or tree.findall('globalVariables/globalVariable/name')
name = element.find("name")
print(element.tag, name.text, element.attrib)
def save_duplicate(self, volumion):
volumion.id = None
volumion.save()
Files.objects.create(**self.cleaned_data, volumio=volumion)
from copy import deepcopy
if form.is_valid():
for vol in Volumion.objects.filter(is_online=True):
new = form.save(commit=False)
new.volumio_id = vol.id
new.set_name()
new.save()
new.save_duplicate(deepcopy(vol))
form = IpForm(request.POST)
if form.is_valid():
data = form.cleaned_data
data['name'] = 'chdir'
IpForm(data)
class Foo(models.Model):
.................
class Meta:
managed = false
def save_data():
file = open("scratch.json", 'r') # Предположим это json
data = json.loads(file.read()) # Загружаем json
file.close()
for row in data:
Scratch.objects.update_or_create(title=row['title'], image=row['image'], price=row['price']) # Название модели и полей надеюсь вы сами подставите
class Order(models.Model):
count = models.PositiveIntegerField(validators=[MinValueValidator(1)], default=1)
import requests
r = requests.get('https://www.google.com/search', {'q': 'Курс доллара'})
game = Game.objects.get(id=gameId)
team = game.team.filter(position__name=Position_name)
if team.exists():
team[0].city = City.objects.get(name=City_name) # Тут стоит сделать более строгую проверку чтобы не было дублей
team[0].save()
class Foo(models.Model):
nested = models.ForeignKey('self', on_delete=CASCADE)
class DriverWorkDay(models.Model):
..................
@property
def days(self):
days = []
if self.monday:
days.append('понидельник')
if self.tuesday:
days.append('вторник')
if self.wednesday:
days.append('среда')
if self.thursday:
days.append('чтверг')
if self.friday:
days.append('пятница')
if self.saturday:
days.append('суббота')
if self.sunday:
days.append('воскресенье')
days = ', '.join([str(x) for x in days])
time = f'(С {self.time_from} по {self.time_to})'
return f'({self.driver.user.last_name} {self.driver.user.first_name} -- {days}, {time})'
class DriverWorkDayAdmin(admin.ModelAdmin):
list_display = ('driver', 'get_work_days', 'get_times')
def get_work_days(self, obj):
return obj.days
............
response = client.get('/categories/')
response.context['name']
response = client.get('/categories/')
response.json()['name']
app = Celery('app', broker='redis://localhost:6379/1',
backend='redis://localhost:6379/1', include=['app.tasks'])
class Category(models.Model):
pass
class Book(models.Model):
categories=models.ManyToManyField(Category)