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

------------------------------------------------------------------------------
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

Reply via email to