Python – Django model with XMLField

Django model with XMLField… here is a solution to the problem.

Django model with XMLField

I’m new to Python/Django.

I’m using django 1.11, python 3.5.2, and Oracle 11g r2

I WANT TO CREATE A DJANGO MODEL FOR TABLE1 IN THE ORACLE DATABASE

MY TABLE1 HAS 4 FIELDS:

ID (TYPE: NUMBER)

NAME (TYPE: VARCHAR2)

LASTNAME (TYPE: VARCHAR2)

INFO (TYPE: XMLTYPE)

This is the format of the xml field:

<?xml version = '1.0' encoding = 'UTF-8'?><extrainfo>
   <info>
      <movie>Titanic</movie>
      <sport>Tennis</sport>
   </info>
   <info>
      <movie>Troy</movie>
      <sport>Soccer</sport>
   </info>
</extrainfo>

I’m creating a django model for table1, but I don’t know how to read xml fields in a database using django.

Here is my model

class Table1(models. Model):
    id = models. IntegerField(primary_key=True)
    name = models. TextField(blank=True)
    lastname = models. TextField(blank=True)
    info = (I dont know what to write here to read the xmltype in db)

class Meta:
        managed = False
        db_table = 'TABLE1' 

What am I going to do?
What’s the best way to do it?

I need to get the xmltype information.

Please help me, I’m stuck.

Thank you.

Solution

Have you tried< a href="https://github.com/theatlantic/django-xml" rel="noreferrer noopener nofollow" >https://github.com/theatlantic/django-xml ? I tried this but it doesn’t seem to save to the database. In addition, you can use TextField as a container for xml and use the lxml parser.

Related Problems and Solutions