Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

django - Writing a cleaner function in python, a parameter to be either a set or a value

I have a struggling with a way of writing this function much cleaner and to make this function for the parameter objs to can be either a QuerySet or an single object, in case when the objs parameter is an set of objects I want to go with the for loop but in the case when is a single object I want to create the date parameter without looping, and also I am not sure if the try block is the good way for checking if the object with these information is already in db or not and if it's not I want to create it with these dates. Any help would be appreciated, thanx!

the function:

def check_recurrent_or_new(user, objs, obj):
    for item in objs:
        if item.created_date.month == 12:
            date = datetime(
                    item.created_date.year + 1, 1, item.created_date.day,
                    item.created_date.hour, item.created_date.minute, item.created_date.second,
                    item.created_date.microsecond
                )
        else:
            date = datetime(
                    item.created_date.year, item.created_date.month + 1, item.created_date.day,
                    item.created_date.hour, item.created_date.minute, item.created_date.second,
                    item.created_date.microsecond
                )
        try:
            obj.objects.get(
                user=user, name=item.name, amount=item.amount,
                created_date=date, category=item.category, recurrent=True
            )
        except:
            obj.objects.create(
                    user=user, name=item.name, amount=item.amount,
                    created_date=date, category=item.category, recurrent=True
question from:https://stackoverflow.com/questions/65874861/writing-a-cleaner-function-in-python-a-parameter-to-be-either-a-set-or-a-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

for the try ,except you can directly use get_or_create method for more information check it here

def check_recurrent_or_new(user, objs, obj):
for item in objs:
    test=item.created_date.month == 12
    date = datetime(
                item.created_date.year + 1 if test else item.created_date.year ,1 if test else item.created_date.month + 1, item.created_date.day,
                item.created_date.hour, item.created_date.minute, item.created_date.second,
                item.created_date.microsecond
            )
    obj,created=obj.objects.get_or_create(
            user=user, name=item.name, amount=item.amount,
            created_date=date, category=item.category, recurrent=True
        )
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...