[Answer]-Django, Deleting or resetting object but not remove from database

0๐Ÿ‘

โœ…

You can set them to None at the beginning of for loop:

for ids in Idlist:
    Loci = None
    Itag = None
    Ncbi = None  

    try:
        Loci = Locus.objects.get(Gen_ID=ids)

    except:
        #no locus info found
        pass
    ....
    #your for loop 
    ....
    # use attributes if objects found, else use ''
    writer.writerow([Itag.Gen_ID if Itag else '',
                   Itag.Solyc if Itag else '', 
                   Ncbi.defname if Ncbi else '', 
                   Loci.Lociname if Loci else ''])
๐Ÿ‘คRohan

1๐Ÿ‘

First of all, ther is a mistake about your approach,

try:
    Loci = Locus.objects.get(Gen_ID=ids)

except:
    #no locus info found
    pass

If there is no data with Gen_ID=ids, then your variable Loci will not be created (if you did not initialize it before), thus,

writer.writerow([Itag.Gen_ID, Itag.Solyc, NCBI.defname, Loci.Lociname])

will raise error since Loci did not created at all.

As for the answer to your question, you can set it to None

writer.writerow([Itag.Gen_ID, Itag.Solyc, NCBI.defname, Loci.Lociname])
Loci = None
Itag = None
NCBI = None

But setting them to None is a better approach if your get functions return no record

for ids in Idlist:
    try:
        Loci = Locus.objects.get(Gen_ID=ids)
    except Locus.DoesNotExist:
        #no locus info found
        Loci = None
    try:    
        Itag = Itag_annotatie.objects.get(Gen_ID=ids)
    except Itag_annotatie.DoesNotExist:
        #no Itag info found
        Itag = None
    try:
        Ncbi = NCBI.objects.get(Gen_ID=ids)
    except NCBI.DoesNotExist:
        #No NCBI info found
        Ncbi = None

    if not Loci:
        continue # end this loop without doing anything else and start the next loop 
    writer.writerow([Itag.Gen_ID if Itag else '', 
                     Itag.Solyc  if Itag else '', 
                     Ncbi.defname if Ncbi else '', 
                     Loci.Lociname # can not be `None`
                   ])

EDIT: Normally, Loci,ItagandNCBIwill be set to new values if your database method get a record, otherwise they will set toNone`.

EDIT 2: I hope i understood correctly. continue statement is used in loops and let you end current loop and start the next loop (if available).

๐Ÿ‘คMp0int

0๐Ÿ‘

In your place, I would have made โ€‹โ€‹a processing code of each object in a separate function:

def write(ids):
    try:
        Loci = Locus.objects.get(Gen_ID=ids)
    except Locus.DoesNotExist:
        Loci = None
    try:    
        Itag = Itag_annotatie.objects.get(Gen_ID=ids)
    except Itag.DoesNotExist:
        Itag = None
    try:
        Ncbi = NCBI.objects.get(Gen_ID=ids)
    except Ncbi.DoesNotExist:
        Ncbi = None

    writer.writerow([Itag.Gen_ID if Itag else None, 
                     Itag.Solyc if Itag else None, 
                     Ncbi.defname if Ncbi else None, 
                     Loci.Lociname if Loci else None])

for ids in Idlist:
     write(ids)

In this case, the variables are local and they do not need to reset.


Another (more clean) variant:

for ids in Idlist:
    Locis = Locus.objects.filter(Gen_ID=ids)[:1]
    Itags = Itag_annotatie.objects.filter(Gen_ID=ids)[:1]
    Ncbis = NCBI.objects.filter(Gen_ID=ids)[:1]
    writer.writerow([Itags[0].Gen_ID if Itags else None, 
                     Itags[0].Solyc if Itags else None, 
                     Ncbis[0].defname if Ncbis else None, 
                     Locis[0].Lociname if Locis else None])

Queryset like Foobar.objects.filter(Gen_ID=ids)[:1] will return an empty list if it is not found, or a list with the first matching entry.

๐Ÿ‘คdefuz

Leave a comment