Return a new list of model objects merging the lists ``list1`` and ``list2`` keeping the original ``list1`` order and discarding duplicates.
(list1, list2, **kwargs)
| 1939 | |
| 1940 | |
| 1941 | def merge_sequences(list1, list2, **kwargs): |
| 1942 | """ |
| 1943 | Return a new list of model objects merging the lists ``list1`` and |
| 1944 | ``list2`` keeping the original ``list1`` order and discarding |
| 1945 | duplicates. |
| 1946 | """ |
| 1947 | list1 = list1 or [] |
| 1948 | list2 = list2 or [] |
| 1949 | merged = [] |
| 1950 | existing = set() |
| 1951 | for item in list1 + list2: |
| 1952 | try: |
| 1953 | if hasattr(item, 'to_tuple'): |
| 1954 | key = item.to_tuple(**kwargs) |
| 1955 | else: |
| 1956 | key = to_tuple(kwargs) |
| 1957 | |
| 1958 | except Exception as e: |
| 1959 | raise Exception(f'Failed to merge sequences: {item}', f'kwargs: {kwargs}') from e |
| 1960 | |
| 1961 | if not key in existing: |
| 1962 | merged.append(item) |
| 1963 | existing.add(key) |
| 1964 | return merged |