с использованием поля serializer-method
может быть вариантом для этого случая. Наша цель - получить product
информацию из category
сериализатора. Так что для этого
class CategorySerializer(serializers.ModelSerializer):
products = serializers.SerializerMethodField()
class Meta:
model = Category
fields = ('') # add relative fields
def get_products(self, obj):
products = obj.product_set.all() # will return product query set associate with this category
response = ProductSerializer(products, many=True).data
return response
Apple added in NSSystemClockDidChangeNotification, part of NSDate, in Snow Leopard (10.6). There doesn't appear to be a way to do it in Leopard (10.5) or earlier. Per the Apple NSDate docs:
Posted whenever the system clock is changed. This can be initiated by a call to settimeofday() or the user changing values in the Date and Time Preference panel. The notification object is null. This notification does not contain a userInfo dictionary.
This doesn't appear to tell you "how much" time has changed. You could possibly calculate that by periodically (say, every 5 seconds in a NSTimer
) capturing the system time with [NSDate date]
, saving it into a variable, and then after NSSystemClockDidChangeNotification
fires, grab the new date and compare the two together using NSDate's timeIntervalSinceDate:
method to get the difference.
Not millisecond or even second accurate, but pretty close.
EDIT: See this post. You could possibly use the UpTime()
C command to grab the system uptime in CPU tics (which you can later convert to seconds). You could use this to figure out by how much time has changed (assuming no system restart or sleep). This works even if the system clock is changed by the user or network time protocol.
Время постоянно перемещается. Уведомление каждый раз измененное текущее время было бы постоянным, впитывающим ЦП потоком уведомлений.
То, что необходимо сделать, получают текущее время в обработчике событий — тот, который получает события, к которым Вы добавляете метку даты. Вы получаете текущее время путем вызова [NSDate date]
.
Я не думаю, что существует единственный способ сделать это из-за различных механизмов, которыми могло измениться время. Но это не было бы очень дорого (слишком дорогой? Не знайте, Вы представили его уже? ;-) для установки NSTimer
однажды секунда, чтобы проверить время и сравнить его с предыдущим значением. Если это не совершенствуется приблизительно на секунду, что-то интересное произошло, и можно уведомить объект аудита.
Если кто-то ищет решение, то знает, что системная дата изменилась с 10.4
OSStatus DateChangeEventHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData)
{
NSLog(@"Event received!\n");
return 0;
}
- (void)SystemTimeChangeHandler
{
EventTypeSpec eventType;
eventType.eventClass = kEventClassSystem;
eventType.eventKind = kEventSystemTimeDateChanged;
EventHandlerUPP eventHandlerUPP =
NewEventHandlerUPP(DateChangeEventHandler);
EventHandlerRef eventHandlerRef = NULL;
(void)InstallApplicationEventHandler(
eventHandlerUPP,
1,
&eventType,
self,
&eventHandlerRef);
}