On 8/6/06, limodou <[EMAIL PROTECTED]> wrote:
> On 8/6/06, Antonio Cavedoni <[EMAIL PROTECTED]> wrote:
> >
> > On 6 Aug 2006, at 6:08, limodou wrote:
> > > A very cool tool. And a question:
> > >
> > > It seems that it can not display OneToOne relationship.
> >
> > I just fixed it to support both OneToOneFields and GenericRelations.
> > I also added lots of samples from Ian's Zyons project:
> >
> >   http://code.djangoproject.com/wiki/DjangoGraphviz
> >
> > Cheers!
>
> Great works!
>
> And if the OneToOne relationship can be displayed just as ForeignKey,
> is this resonable?
>
> And you can also add these code at the top of your script:
>
> import settings
> from django.core.management import setup_environ
> setup_environ(settings)
>
> So you don't need to setup DJANGO_SETTING_MODULE env variable any
> more. But the settings module should be settings.
>
> Good work, I like it.
>
> And other suggestion is: can methods be displayed in dot?
>
It seems that _meta.one_to_one_field is not a list or tuple. So for
field in o._meta.one_to_one_field will be failed. And I think to deal
with OneToOne relationship should just like deal with ForeignKey:

      for field in o._meta.fields:
         if isinstance(field, ForeignKey):
            _rel = ' %s -> %s [label="%s"];' % \
                (o.__name__, field.rel.to.__name__, type(field).__name__)
            if _rel not in rel:
               rel.append(_rel)
         elif isinstance(field, OneToOneField):
               _rel = '  %s -> %s [label="%s"] [arrowhead=none
arrowtail=none];' % \
                   (o.__name__, field.rel.to.__name__, type(field).__name__)
               if _rel not in rel:
                  rel.append(_rel)

And I changed your code:

#!c:\python24\bin\python.exe
"""Django model to DOT (Graphviz) converter
by Antonio Cavedoni <[EMAIL PROTECTED]>

Make sure your DJANGO_SETTINGS_MODULE is set to your project and call
the script like this:

  $ python modelviz.py <app_label> > <filename>.dot

Example:

  $ python modelviz.py camera > foo.dot
"""
import settings
from django.core.management import setup_environ
setup_environ(settings)

from django.db import models
from django.db.models import get_models
from django.db.models.fields.related import \
    ForeignKey, OneToOneField, ManyToManyField
from django.db.models.fields.generic import GenericRelation

def generate_dot(app_label):
   app = models.get_app(app_label)

   print "digraph %s {" % ('"' + app.__name__ + '"')
   print """  fontname = "Helvetica"
  fontsize = 8

  node [
    fontname = "Helvetica"
    fontsize = 8
    shape = "record"
  ]
   edge [
    fontname = "Helvetica"
    fontsize = 8
  ]
"""
   for o in get_models(app):
      nodes = []
      nodes.append("""  %s [
    label = "{%s |""" % (o.__name__, o.__name__))

      # members
      for field in o._meta.fields:
         nodes.append('%s : %s\\l' % (field.name, type(field).__name__))

      if o._meta.many_to_many:
         for field in o._meta.many_to_many:
            nodes.append('%s : %s\\l' % (field.name, type(field).__name__))

#      if o._meta.one_to_one_field:
#         for field in o._meta.one_to_one_field:
#            nodes.append('%s : %s\\l' % (field.name, type(field).__name__))

      nodes.append(""" }"\n]\n""")
      print "".join(nodes)

      # relations
      rel = []
      for field in o._meta.fields:
         if isinstance(field, ForeignKey):
            _rel = ' %s -> %s [label="%s"];' % \
                (o.__name__, field.rel.to.__name__, type(field).__name__)
            if _rel not in rel:
               rel.append(_rel)
         elif isinstance(field, OneToOneField):
            _rel = '  %s -> %s [label="%s"] [arrowhead=none
arrowtail=none];' % \
               (o.__name__, field.rel.to.__name__, type(field).__name__)
            if _rel not in rel:
               rel.append(_rel)

      if o._meta.many_to_many:
         for field in o._meta.many_to_many:
            if isinstance(field, ManyToManyField):
               _rel = '  %s -> %s [label="%s"] [arrowhead=normal
arrowtail=normal];' % \
                   (o.__name__, field.rel.to.__name__, type(field).__name__)
               if _rel not in rel:
                  rel.append(_rel)

            if isinstance(field, GenericRelation):
               _rel = '  %s -> %s [label="%s"] [style="dotted"]
[arrowhead=normal arrowtail=normal];' % \
                   (o.__name__, field.rel.to.__name__, type(field).__name__)
               if _rel not in rel:
                  rel.append(_rel)

#      if o._meta.one_to_one_field:
#         for field in o._meta.one_to_one_field:
#            if isinstance(field, OneToOneField):
#               _rel = '  %s -> %s [label="%s"] [arrowhead=none
arrowtail=none];' % \
#                   (o.__name__, field.rel.to.__name__, type(field).__name__)
#               if _rel not in rel:
#                  rel.append(_rel)

      print "\n".join(rel)

   print "}"

if __name__ == "__main__":
   import sys
   app_label = sys.argv[1]
   generate_dot(app_label)


And I found o._meta.one_to_one_field seems not useful.

-- 
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to