has_value()#

relationalai.InstanceProperty
#InstanceProperty.has_value() -> InstanceProperty

Checks if a property has a value and is not None. Useful for filtering objects where the property is defined and has a non-null value. Must be called in a rule or query context.

Returns#

An InstanceProperty object representing the property with a value check applied.

Example#

Use has_value() to filter objects with defined property values:

#import relationalai as rai

# Create a model named "books" with a Book type.
model = rai.Model("books")
Book = model.Type("Book")

# Add some books to the model.
with model.rule():
    # Each call to `Book.add` creates a book with title, author, and year_published properties.
    Book.add(title="Foundation", author="Isaac Asimov", year_published=1951)
    Book.add(title="Foundation and Empire", author="Isaac Asimov", year_published=1952)
    Book.add(title="Dune", author="Frank Herbert")  # Note that year_published is not set.

# Get books that have a year published.
with model.query() as select:
    book = Book()
    book.year_published.has_value()  # Filter books that have a year published.
    response = select(book.title, book.year_published)

print(response.results)
# Output:
#                    title  year_published
# 0             Foundation            1951
# 1  Foundation and Empire            1952

If you don’t use has_value(), the output includes books with None values for the year_published property:

## Get books and their year published without filtering.
with model.query() as select:
    book = Book()
    response = select(book.title, book.year_published)

print(response.results)
# Output:
#                    title  year_published
# 0                   Dune             NaN
# 1             Foundation          1951.0
# 2  Foundation and Empire          1952.0

See Also#