Can someone point me to where this is explained in the docs (1.6)?

I have a ManyToMany relationship in my models between Author and Paper.

I am processing an XML file to create records for Papers.

I want to check if an Author exists and if not create the record.

This was quite easy for Foreign Key relationships.  But I can't get my head
around how to do this for ManyToMany.  All of my Authors are created
correctly.  But none are automatically selected for the Papers.

Some code:

class AuthorManager(models.Manager):
    def create_author(self, name, email, affl):
        a = self.create(name=name, email=email, affiliation=affl)
        a.save()
        return a

class Author(models.Model):
    name = models.CharField("Name", max_length=255, db_index=True,
null=True, blank=True, help_text="")
    email = models.EmailField("Email", max_length=255, null=True,
blank=True, help_text="")
    affiliation = models.CharField("Affiliation", max_length=255,
null=True, blank=True, help_text="")

    objects = AuthorManager()

    def __str__(self):
        return self.name


def import_pubmed_xml(obj, f):
...
        p =
Paper.objects.create_paper(ptitle,rid,jid,jyear,jvol,jissue,lang)

        #authors
        affl = article.xpath("./Affiliation/text()")
        if affl: affl = affl[0]
        else: affl = 'unknown'
        affl = affl.strip()

        authorlist = article.xpath("./AuthorList")
        for authorentry in authorlist:
            for author in authorentry:
                lname = author.xpath("./LastName/text()")
                if lname:
                    lname = lname[0]
                else:
                    lname = 'unknown'
                fname = author.xpath("./ForeName/text()")
                if fname:
                    fname = fname[0]
                else:
                    fname = 'unknown'
                initials = author.xpath("./Initials/text()")
                if initials:
                    initials = initials[0]
                else:
                    initials = ''

                a_name = (lname +', '+fname + ' ' +initials).strip()
                print(a_name)
                email = ''
                #need to lookup Author by name then check affiliation. If
not found then create it.
                try:
                    a = Author.objects.get(name=a_name)
                    p.authors.id = a.id
                    p.save()
                except ObjectDoesNotExist:
                    a = Author.objects.create_author(a_name, email, affl)
                    p.authors.id = a.id
                    p.save()
===================================================

Thanks in Advance,
Tim





-- 
MLHIM VIP Signup: http://goo.gl/22B0U
============================================
Timothy Cook, MSc           +55 21 94711995
MLHIM http://www.mlhim.org
Like Us on FB: https://www.facebook.com/mlhim2
Circle us on G+: http://goo.gl/44EV5
Google Scholar: http://goo.gl/MMZ1o
LinkedIn Profile:http://www.linkedin.com/in/timothywaynecook

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2B%3DOU3XyRXaR%2BBt_QsryiXpEv15%2BTCmWYyHzTnOobnVWCS4xiA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to