The empty value for a field linked with a Many2One relation on reports during printing is it a bug

Hi i want to know : is the empty value for a field linked with a Many2One relation on reports during printing is it a bug !!!
this is my code :
on the ClassA , i was called a field1:

  class ClassA :    
     __name__ = 'sale.class_a'                                                                                                                     
  field1 = fields.Many2One('sale.field1', 'Code', ondelete='CASCADE', select=True )

reciprocally the class called by this field was:

class SaleCode(ModelSQL, ModelView):
    _order_name = 'rec_name'
    code = fields.Char('Code', translate=True,required=True
      )
    Name = fields.Char('Name', translate=True
        )

    def get_rec_name(self, code):
        return self.code


    @classmethod
    def __setup__(cls):
        super(SaleCode, cls).__setup__()
        cls._order.insert(0, ('rec_name', 'ASC'))

on report:

    <table:table-cell table:style-name="Table1.D5" office:value-type="string">
          <text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;class_a.field1.code&quot;&gt;</text:placeholder></text:p>
      <text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;class_a.field1.rec_name&gt;</text:placeholder></text:p>
      <text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p>
        </table:table-cell>

My problem that when i When I leave the field1 blank (without fill it out)

there was an error :

  File "/Genshi-0.7.5-py3.8.egg/genshi/template/eval.py", line 397, in undefined
    raise UndefinedError(key, owner=owner)
genshi.template.eval.UndefinedError: None has no member named "code"

Well it is a bug but in your code. You can not access attribute if there is no record. You must define explicitly a fallback behavior if the record is empty.

should i add that in the code xml of the report or in the code !
have you an example i can follow it and see where i am wrong!

Usually we use such expression in this case: class_a.field1.rec_name if class_a.field1 else ''
But you may want to display a different fallback value than an empty string.

thank you for the The quick answer , but i doesn’t also work with me , i try this yesterday but the same error shown

File "/Genshi-0.7.5-py3.8.egg/genshi/template/base.py", line 291, in _eval_expr
        retval = expr.evaluate(ctxt)
      File "/Genshi-0.7.5-py3.8.egg/genshi/template/eval.py", line 160, in evaluate
        return eval(self.code, _globals, {'__data__': data})
      File "<string>", line 390, in <Expression '__relatorio_escape_invalid_chars(class_a.field1.code)'>
      File "/Genshi-0.7.5-py3.8.egg/genshi/template/eval.py", line 309, in lookup_attr
        val = cls.undefined(key, owner=obj)
      File "/Genshi-0.7.5-py3.8.egg/genshi/template/eval.py", line 397, in undefined
        raise UndefinedError(key, owner=owner)
    genshi.template.eval.UndefinedError: None has no member named "code"

i do no why it keeps talking in the error about code and me i register it class_a.field1.rec_name (no word "code)

this is the code i put it as u said :
<table:table-cell table:style-name="Table1.D5" office:value-type="string"> <text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;if test=&quot;class_a.field1.rec_name if class_a.field1 else "" &quot;&gt;</text:placeholder></text:p> <text:p text:style-name="Table_20_Contents"><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p> </table:table-cell>
is their another special step i should do it in the code python !

Look at your class SaleCode, you have a function get_rec_name there. This is called when you want to show the class_a.field1.rec_name. So it wants to return the contents of the field code, but it can’t. So you have to add the fallback their, rather then in your report.

I put it like this ( display empty string if the field1 is None)

    def get_rec_name(self, name):
           if self.field1 is not None:
               return self.field1.rec_name
           else:
             return" "

but the same error was shown.

Sorry my mistake :frowning_face: I didn’t read too carefully.

In you report you have the wrong if statement. You have

&lt;if test=&quot;class_a.field1.rec_name if class_a.field1 else "" &quot;&gt;

I normally wrap it in the same manner as the One2Many. Something like

<text:p><text:placeholder text:placeholder-type="text">&lt;if test=&quot;class_a.field1&quot;&gt;</text:placeholder></text:p>
<text:p><text:placeholder text:placeholder-type="text">&lt;class_a.field1.rec_name&gt;</text:placeholder></text:p>
<text:p><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p>

Hi thank you for replying,

i put it like u said (without using else),

<text:p><text:placeholder text:placeholder-type="text">&lt;if test=&quot;class_a.field1&quot;&gt;</text:placeholder></text:p>
<text:p><text:placeholder text:placeholder-type="text">&lt;class_a.field1.rec_name&gt;</text:placeholder></text:p>
<text:p><text:placeholder text:placeholder-type="text">&lt;/if&gt;</text:placeholder></text:p>

but always the same error when i try to print the report .

File "<string>", line 390, in <Expression '__relatorio_escape_invalid_chars(class_a.field1.code)'>
genshi.template.eval.UndefinedError: None has no member named "code"

The only thing I can say, is that you:

  1. still have somewhere in your report a link to the code field
  2. changing the wrong report

You can test this by removing basically everything from your report. If the errors still comes up, you are editing the wrong report or Tryton uses it’s cache (which I think can be cleared by restarting Tryton server)

1 Like