Sven,

I recommend that you do *not* read this email.  The reason is that I
have been confused about this issue before, and I am (below) trying
to think it through and eliminate some of that confusion.

Maybe tomorrow I'll have a less twisted reply.

Let's see if we can get my head straight on this one.

A recipe can have a single "categories" and that "categories"
element can contain multiple "category" elements.

Do we agree so far?

If so, the question is: How do we represent that in a Django model?

In what is currently generated, the "Recipe" model has a ForeignKey
to a "CategoriesType" model.  This means that a record in the
"Recipe" table (relation) can point to (refer to, be joined with?)
multiple records in the "CategoriesType" table.

Which is what we want, right?  Or am I twisting the fact so that
they fit what I want to see?

Look at this snippet from the Django documentation (at
https://docs.djangoproject.com/en/1.11/topics/db/models/):

        from django.db import models

        class Musician(models.Model):
                first_name = models.CharField(max_length=50)
                last_name = models.CharField(max_length=50)
                instrument = models.CharField(max_length=100)

        class Album(models.Model):
                artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
                name = models.CharField(max_length=100)
                release_date = models.DateField()
                num_stars = models.IntegerField()

Does the above mean that an "Album" can have multiple musicians, that is
references to multiple records in the "Musician" table?  Or, does it
mean that a musician can have multiple albums?

Here is another snippet from the same Web page:

    Many-to-one relationships¶

    To define a many-to-one relationship, use django.db.models.ForeignKey. You
    use it just like any other Field type: by including it as a class attribute
    of your model.

    ForeignKey requires a positional argument: the class to which the model is
    related.

    For example, if a Car model has a Manufacturer – that is, a Manufacturer
    makes multiple cars but each Car only has one Manufacturer – use the
    following definitions:

        from django.db import models

        class Manufacturer(models.Model):
            # ...
            pass

        class Car(models.Model):
            manufacturer = models.ForeignKey(Manufacturer, 
on_delete=models.CASCADE)
            # ...

In our case, we want a "Recipe" to have multiple instances of
"CategoriesType".  So, the should place the ForeignKey in the
"CategoriesType" model.

So, perhaps the rule we should follow is: If you have Model-A and
Model-B and if you want a Model-A to have multiple instances of
Model-B, then put the ForeignKey in Model-B (referring to Model-A).

What do you think?

It worries me that I seem to recall I've been confused about this
before.

More on this tomorrow.

Dave

On Mon, Oct 23, 2017 at 08:33:36AM +0200, Sven Prevrhal wrote:
> I have an XSD with
> 
>    <xs:element name="recipe">
>      <xs:complexType>
>        <xs:sequence>
> 
>          <xs:element name="title" type="xs:string"/>
> 
>          <xs:element name="categories">
>            <xs:complexType>
>              <xs:sequence>
>                <xs:element name="category" type="xs:string" minOccurs="1"
> maxOccurs="unbounded"/>
>              </xs:sequence>
>            </xs:complexType>
>          </xs:element>
>          ....
>        </xs:sequence>
>      </xs:complexType>
>    </xs:element>
> 
> The Django models I get from that are
> 
> 
> class Recipe(models.Model):
>     title = models.CharField(max_length=1000, )
>     categories = models.ForeignKey(
>         "CategoriesType",
>         related_name="recipe_categories_categoriesType",
>     )
>     ....
> 
> and
> 
> class CategoriesType(models.Model):
>     category = models.CharField(max_length=1000, )
>     ....
> 
> This seems wrong to me. I would have thought Category gets a ManyToManyField
> 
> to Recipe. The way it comes out of generateDS it looks like a
> CategoriesType has many Recipes. Or perhaps my XSD is wrong?
> 
> Cheers,
> Sven

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



-- 

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