Kirk,
Trying to think this through.
On Fri, Jun 23, 2017 at 11:32:01AM -0700, Kirk Tarou wrote:
> For example, in the Django models documentation
> <https://docs.djangoproject.com/en/1.11/topics/db/models/#fields> they have
> a Musician model and an Album model. The Album contains a foreign key to a
> Musician model.
>
> What would that XSD need to look like in order for the corresponding Django
> model to look like the example?
Ah. Interesting question. And, the parallel question is: what
might the XML instance document look like so that it could represent
the data in that musician/album database? Once we produce a
representation in XML, and an XML schema that describes it, we can
talk about whether the django support in generateDS produces usable
models. I'll give it a try:
The musicians might look pretty much as you'd expect. Example:
<musician musician_id="001" name="Django Reinhardt">
...
</musician>
The albums might each contain a list of references to the musicians
that perform on that album. Imagine that each musician had an ID
attribute. Example:
Then the album element could contain a list of musicians. Example:
<album name="..." musicians="001 008 012">
...
</album>
The schema for the musicians and album might look something like
this:
<xs:complexType name="musicianType">
...
<xs:attribute name="id" type="ID"/>
</xs:complexType>
<xs:complexType name="albumType">
...
<xs:attribute name="name" type="xs:string"/>
<xs:attribute name="musicians" type="musicianListType"/>
</xs:complexType>
<xs:simpleType name="musicianListType">
<xs:list itemType="IDREF"/>
</xs:simpleType>
Or, maybe you want to use a list of elements to represents the
musicians on an album. Then the XML schema might look like this:
<!-- musicianType as above -->
<xs:complexType name="altAlbumType">
<xs:sequence>
<xs:element name="musician" type="IDREF"
minOccurs="0" maxOccurs="unbounded"/>
...
</xs:sequence>
</xs:complexType>
You'd need to assign an ID to each musician.
The ID would be a column in the musician table in the database. Is
that what is called a primary key?
I've attached an XML schema and also the models.py file that the
generateDS django support generated.
What do you think? Does the above (and the attached) seem
reasonable?
Oh, and, depending on your needs, you may have to write the code
that (1) reads XML and stores it in the Django database and (2)
dumps the Django database to XML. But, then we'd have that task no
matter what XML representation is chosen.
Dave
On Fri, Jun 23, 2017 at 11:32:01AM -0700, Kirk Tarou wrote:
> Hi Dave,
>
> Thank you so much for taking the time to look into this. I'm going to go
> through your email and example today and make sure I understand things.
>
> But, after a cursory look at the example you provided, I wanted to clarify
> something. I'm not talking about inheritance, but simple one-to-many
> relationships. Perhaps my initial email wasn't clear.
>
> For example, in the Django models documentation
> <https://docs.djangoproject.com/en/1.11/topics/db/models/#fields> they have
> a Musician model and an Album model. The Album contains a foreign key to a
> Musician model.
>
> What would that XSD need to look like in order for the corresponding Django
> model to look like the example?
>
> -Kirk
>
> On Thu, Jun 22, 2017 at 3:53 PM, Dave Kuhlman <dkuhl...@davekuhlman.org>
> wrote:
>
> > Kirk,
> >
> > These pages seems to be helpful on this topic:
> >
> > - https://docs.djangoproject.com/en/1.11/topics/db/models/
> > - https://docs.djangoproject.com/en/1.11/ref/models/fields/
> > #django.db.models.ForeignKey
> >
> > I'm reading ...
> >
> > An example might be helpful. Here is the relavant parts of a schema::
> >
> > <xs:element name="programmer" type="programmerType">
> > <xs:annotation><xs:documentation>
> > A programmer type of person. Programmers are very special
> > but also a little shy.
> > </xs:documentation></xs:annotation>
> > </xs:element>
> > <xs:complexType name="programmerType" mixed="0">
> > <xs:complexContent>
> > <xs:extension base="personType">
> > <xs:sequence>
> > <!--
> > <xs:element name="description" type="xs:string"/>
> > -->
> > <xs:element name="email" type="xs:string"/>
> > <xs:element name="elposint" type="xs:positiveInteger"/>
> > <xs:element name="elnonposint"
> > type="xs:nonPositiveInteger"/>
> > <xs:element name="elnegint" type="xs:negativeInteger"/>
> > <xs:element name="elnonnegint"
> > type="xs:nonNegativeInteger"/>
> > <xs:element name="eldate" type="xs:date"/>
> > <xs:element name="eldatetime" type="xs:dateTime"/>
> > <xs:element name="eldatetime1" type="xs:dateTime"/>
> > <xs:element name="eltoken" type="xs:token"/>
> > <xs:element name="elshort" type="xs:short"/>
> > <xs:element name="ellong" type="xs:long"/>
> > <xs:element name="elparam" type="paramType"/>
> > <xs:element name="elarraytypes" type="ArrayTypes"/>
> >
> > </xs:sequence>
> > <xs:attribute name="language" type="xs:string" />
> > <xs:attribute name="area"/>
> > <xs:attribute name="attrposint" type="xs:positiveInteger"/>
> > <xs:attribute name="attrnonposint"
> > type="xs:nonPositiveInteger"/>
> > <xs:attribute name="attrnegint" type="xs:negativeInteger"/>
> > <xs:attribute name="attrnonnegint"
> > type="xs:nonNegativeInteger"/>
> > </xs:extension>
> > </xs:complexContent>
> > </xs:complexType>
> >
> > <xs:element name="python-programmer" type="python-programmerType">
> > <xs:annotation><xs:documentation>
> > A Python programmer type of person. Programmers are very special
> > and Python programmers are especially wonderful kinds
> > of people.
> > </xs:documentation></xs:annotation>
> > </xs:element>
> > <xs:complexType mixed="0" name="python-programmerType">
> > <xs:complexContent>
> > <xs:extension base="programmerType">
> > <xs:sequence>
> > <xs:element name="favorite-editor" type="xs:string"/>
> > <xs:element name="flowvalue" type="FlowType"/>
> > <xs:element name="drcs" type="xs:string"/>
> > </xs:sequence>
> > <xs:attribute name="nick-name"/>
> > <xs:attribute name="drcs" type="xs:string" />
> > <xs:attribute name="gui_developer" type="xs:boolean"/>
> > </xs:extension>
> > </xs:complexContent>
> > </xs:complexType>
> >
> >
> > And, here is the generated model::
> >
> > class programmerType_model(models.Model):
> > personType = models.ForeignKey("personType_model")
> > language = models.CharField(max_length=1000, blank=True,
> > null=True)
> > area = models.CharField(max_length=1000, blank=True, null=True)
> > attrposint = models.IntegerField(blank=True, null=True)
> > attrnonposint = models.IntegerField(blank=True, null=True)
> > attrnegint = models.IntegerField(blank=True, null=True)
> > attrnonnegint = models.IntegerField(blank=True, null=True)
> > email = models.CharField(max_length=1000, )
> > elposint = models.IntegerField()
> > elnonposint = models.IntegerField()
> > elnegint = models.IntegerField()
> > elnonnegint = models.IntegerField()
> > eldate = models.DateField()
> > eldatetime = models.DateTimeField()
> > eldatetime1 = models.DateTimeField()
> > eltoken = models.CharField(max_length=1000, )
> > elshort = models.IntegerField()
> > ellong = models.IntegerField()
> > elparam = models.ForeignKey(
> > "paramType_model",
> > related_name="programmerType_elparam_paramType",
> > )
> > elarraytypes = models.CharField(max_length=1000, )
> >
> > def __unicode__(self):
> > return "id: %s" % (self.id, )
> >
> >
> > class python_programmerType_model(models.Model):
> > programmerType = models.ForeignKey("programmerType_model")
> > nick_name = models.CharField(max_length=1000, blank=True,
> > null=True)
> > drcs_attr = models.CharField(max_length=1000, blank=True,
> > null=True)
> > gui_developer = models.BooleanField(blank=True, null=True)
> > favorite_editor = models.CharField(max_length=1000, )
> > flowvalue = models.IntegerField()
> > drcs = models.CharField(max_length=1000, )
> >
> > def __unicode__(self):
> > return "id: %s" % (self.id, )
> >
> > [the above example is taken from Demos/People/people.xsd in the
> > generateDS distribution.]
> >
> > As you mentioned, the ForeignKey is in the child or subclass model.
> >
> > In this example, python_programmerType extends (or inherits from or
> > is a subclass of) programmerType.
> >
> > So, we have used ``models.ForeignKey`` to create a relationship
> > from a python_programmerType record to a programmerType record.
> > Effectively, we are saying that in order to get all the data for a
> > python_programmer, you need to query and read a record from both the
> > python_protrammerType table and the programmerType table. Am I
> > right about this?
> >
> > I'm guessing that this is not the *best* way to do this, but it does
> > seem to be one way.
> >
> > The following section in the Django docs seems to be saying
> > that there are multiple ways to encode inheritance:
> > https://docs.djangoproject.com/en/1.11/topics/db/models/#model-inheritance
> >
> > If you have a better way, I'm willing to look at it. You'd have to
> > send me an example of the schema and the Django model that you'd
> > like to see generated from it.
> >
> > But, unless we can show that the code currently being generated is
> > *wrong*, you'd have to push pretty hard to get me to change it.
> >
> > I'm certainly willing to listen and learn about this. As I said,
> > I'm nowhere near being an expert on Django and it's ORM.
> >
> > Hope this help.
> >
> > Dave
> >
> >
> > On Wed, Jun 21, 2017 at 11:45:46AM -0700, Kirk Tarou wrote:
> > > Hi Dave,
> > >
> > > I've recently started using your project to generate Django models from
> > an
> > > xsd file and find it quite useful except for one thing: The foreign keys
> > > are incorrect in the data model I'm generating. Instead of the child
> > object
> > > having a 'models.ForeignKey' member that points to the parent object, the
> > > parent object has an entry that points to the child object.
> > >
> > > Have you had anyone else contact you with this issue?
> > >
> > > --
> > > -Kirk
> >
> > --
> >
> > Dave Kuhlman
> > http://www.davekuhlman.org
> >
>
>
>
> --
> -Kirk
--
Dave Kuhlman
http://www.davekuhlman.org
from django.db import models
class albumType_model(models.Model):
artist = models.CharField(max_length=1000, blank=True, null=True)
title = models.CharField(max_length=1000, )
genre = models.CharField(max_length=1000, )
def __unicode__(self):
return "id: %s" % (self.id, )
class alternativeAlbumType_model(models.Model):
title = models.CharField(max_length=1000, )
genre = models.CharField(max_length=1000, )
artist = models.ForeignKey(
"artistCollectionType_model",
related_name="alternativeAlbumType_artist_artistCollectionType",
)
def __unicode__(self):
return "id: %s" % (self.id, )
class artistCollectionType_model(models.Model):
artistRef = models.CharField(max_length=1000, blank=True, null=True)
def __unicode__(self):
return "id: %s" % (self.id, )
class musicianType_model(models.Model):
musician_id = models.CharField(max_length=1000, blank=True, null=True)
name = models.CharField(max_length=1000, )
instrument = models.CharField(max_length=1000, )
def __unicode__(self):
return "id: %s" % (self.id, )
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="musicianType">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="instrument" type="xs:string"/>
</xs:sequence>
<xs:attribute name="musician_id" type="xs:ID"/>
</xs:complexType>
<xs:complexType name="albumType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="genre" type="xs:string"/>
</xs:sequence>
<xs:attribute name="artist" type="artistListType"/>
</xs:complexType>
<xs:simpleType name="artistListType">
<xs:list itemType="xs:IDREF"/>
</xs:simpleType>
<xs:complexType name="alternativeAlbumType">
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="genre" type="xs:string"/>
<xs:element name="artist" type="artistCollectionType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="artistCollectionType">
<xs:sequence>
<xs:element name="artistRef" type="xs:IDREF"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users